Drawing a right triangle with loops [duplicate] - python

This question already has answers here:
Using python, How can I make a pyramid using for loops?
(3 answers)
Closed 6 years ago.
I have spent the last half hour playing around and I still don't know how to make it so a character is added each time it loops.
This picture can better explain it better of what I mean.
here's my code:
triangle_char = input("Enter a character: \n")
triangle_height = int(input("Enter triangle height: \n"))
for row in range(triangle_height):
print(triangle_char)

(My previous answer was not very well thought out, so edit:)
You just need to keep adding your triangle_char to a string. Start by declaring an empty string
string = ""
and then every iteration of the loop, add one character to it
string += triangle_char
For example:
triangle_char = input("Enter a character: \n")
triangle_height = int(input("Enter triangle height: \n"))
string = ""
for row in range(triangle_height):
string += triangle_char
print(string)
Will give you the result you are looking for

Related

Basic math in python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.

Program printing a number a certain times instead of multiplying [duplicate]

This question already has answers here:
Why does multiplication repeats the number several times? [closed]
(7 answers)
Closed 1 year ago.
I’ve written a piece of code that instead of print the product, prints a number a certain number of times. Whats wrong with it?
twelve = 12
name = input("What is your name? \nAnswer: ")
print("Cool name!")
nums = input("\n\nHow much pocket money did you receive last month?\nAnswer: ")
total = nums * twelve
print("\n\nI think you get ", total + " pounds in pocket money per year! Nice!")
The reason is that your nums variable is a string, which is the default with all Python inputs. Try converting it to int:
nums = int(input(...))
Or float if you are inputting a floating point number.

isinstance() not working the way I think it should [duplicate]

This question already has answers here:
Read an integer list from single line input along with a range using list comprehension in Python 3
(2 answers)
Closed 3 years ago.
My program requires a user input of a list of two elements, so to check if those conditions are satisfied I used the following code:
start = input('Enter you start location.\nE.g. Enter "[2,5]" for x-coordinate 2 and y-coordinate
5.')
while isinstance(start, list) == False or len(start) != 2:
start = input('Try again.')
This will never exit the while loop no matter what I input. Why?
Because your start variable turns out to be a string:start = "[2,5]", which is not a list. You can ask the user to input e.g 2,3,
then you get "2,3". You then can split it to a list using start.split(',')
Absolutely not recommanded for obvious security risk, but you can use eval.
start = eval(input('Enter you start location.\nE.g. Enter "[2,5]" for x-coordinate 2 and y-coordinate 5.'))
A prefered way is by using split, but in this case ask the user to enter coordinate separated by a coma.
start = input('Enter you start location.\nE.g. Enter "2,5" for x-coordinate 2 and y-coordinate 5.')
start = start.split(",")
Edit as recommanded by #soyapencil comments
inp_str = input('Enter you start location.\nE.g. Enter "[2,5]" for x-coordinate 2 and y-coordinate 5.')
start = [int(i) for i in iter(eval(inp_str,{}))]

How to subtract 2 integers within a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am probably just tackling this the wrong way but at line 2 I am trying to subtract 2 user inputs within a print string.
BTW this is just for a nim sum game, where the user chooses the amount and goes first, then plays the computer.
def setup(startamt, userturn):
print(str("there are " + startamt - userturn + " balls left"))
def main():
startamt = input("How many balls do you want to start with?(15 or more) ")
userturn = input("How many balls will you take?(1-4) ")
setup(startamt, userturn)
The first problem is that input() returns a string (str) object, so to do a calculation you need to convert them to integers (int()) or floating point (float()) numbers.
Since you are on 3.6 then you can use Literal string interpolation. For example:
startamt = "52"
userturn = "9"
print(f"there are {str(int(startamt) - int(userturn))} balls left")
The preceding f indicates we are using interpolation, which is done inside the { }.
In your second line, the startamt - userturn are each being evaluated as strings.
You can either do:
print("there are " + int(startamt) - int(userturn) + " balls left"))
or using string formatting which is more Pythonic:
print("there are {} balls left".format(int(startamt) - int(userturn)))
This seems to work
def setup(startamt, userturn):
print("there are", str((int(startamt) - int(userturn))), "balls left"
def main():
startamt = input("How many balls do you want to start with?(15 or more) ")
userturn = input("How many balls will you take?(1-4) ")
setup(startamt, userturn)
main()

How to keep increasing variable number in loop inpython [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 months ago.
I need to create a code where the user can input a certain number of courses, and then it will take the gpa of them, but how can I change the variable name in the loop?
I have this so far
number_courses= float(input ("Insert the number of courses here:"))
while number_courses>0:
mark_1= input("Insert the letter grade for the first course here: ")
if mark_1=="A+" :
mark_1=4.0
number_courses= number_courses-1
If I want to change the variable name of mark_one to something different each time I go through the loop, what is the simplest way I can do this? And also is it possible to change it in my input statement to ask for first, second, third... as I go through the loop? I have tried searching on google, but none of the answers I can understand as their code is far behind my level or they didn't seem to answer what I needed either. Thanks.
You want to use a list or something similar to gather the input values:
number_courses=input("Insert the number of courses here: ")
marks = []
while number_courses>0:
mark = input("Insert the letter grade for the first course here: ")
if mark == "A+":
mark = 4.0
marks.append(mark)
number_courses -= 1
print marks
Use a dictionary:
number_courses= float(input ("Insert the number of courses here:"))
marks = {'A+':4, 'A':3.5, 'B+':3}
total_marks = 0
while number_courses:
mark_1= input("Insert the letter grade for the first course here: ")
if mark_1 in marks:
total_marks += marks[mark_1] #either sum them, or append them to a list
number_courses -= 1 #decrease this only if `mark_1` was found in `marks` dict

Categories