This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I am trying to make a menu (only 25% complete as of now) and whenever I input a number for example 3, the list z outputs a value of z = ['']
print("Welcome to Kushagra's Pizzeria!")
z = []
a = ""
print('''
Please select a size-
1.Small
2.Medium
3.Large
''')
y = input("-->")
if y == 1:
a = "Small"
elif y == 2:
a = "Medium"
elif y == 3:
a = "Large"
z.append(a)
print(z)
You either convert the input to int or look for the string value
y == "1"
or
int(input("-->"))
input() returns a string. you need to convert it to an integer by calling the int method
y = int(input("-->"))
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
python TypeError not all arguments converted during string formatting [duplicate]
(2 answers)
Closed 11 months ago.
The program should print the entries which are divided into three without the remainder. The problem is "TypeError : not all arguments converted during string formatting python " in the bottom
numbers = [ ]
while True:
inputNumber = (input("Enter a number if you want to terminate this, please tap on 'q' : "))
if inputNumber == "q":
break
numbers.append(inputNumber)
sum = 0
for i in numbers:
sum+=int(i)
print("Sum of the inputs : ", sum)
#unexecutable lines
for i in numbers:
if(i%3 == 0):
print(i)
Variable i is in string format.
Hence type cast it to int before carrying out calculation:
#unexecutable lines
for i in numbers:
if(int(i)%3 == 0):
print(i)
This question already has answers here:
Check if a number is odd or even in Python [duplicate]
(6 answers)
Closed 1 year ago.
I would just like a tip on how to send even numbers to the left and odd numbers to the right, I've been trying for a long time and I can't, the subject is composed lists, I appreciate any help. Simplified code below without loop or conditional structure.
test = [[],[]]
num = int (input("Type it : "))
test.append()
print(test)
test = [[], []]
num = input("Enter a number or quit")
while num != "quit": # execute code as long as num is not quit
num = int(num) # convert num to a number (until here, it's a string !!!)
if num % 2 == 0: # num is even
test[0].append(num)
else: # num is odd
test[1].append(num)
print(test)
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
im trying to create a multiple values calculator in Python, using While, For/In sentences with lists
numbers = []
out = 0
while out == 0:
numbers.append(int(input('Add a number: ')))
out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
if out2 == 1:
out == 1
#In theory if out == 1 the while loop should end and go to:
for add in numbers:
add = numbers
print(add)
I tried using While Not sentence, but i get an obvius mistake. I suppose this is a pretty dumb error of comprehention i have, but i can't really get what am i doing wrong. I'll be very glad if u help me with this.
First of all, variable assignment should be done with = not ==. Your problem is that in the second input() (where you let user choose will he/she stay or exit), you need to convert the input to int from string first, OR check by the string representation of the number (fail-safe):
while out == 0:
numbers.append(int(input('Add a number: ')))
out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
if out2 == '1':
out = 1
However, best way to break out a loop is to use break:
while out == 0:
numbers.append(int(input('Add a number: ')))
out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
if out2 == '1':
break
Apart from the errors pointed out in the other 2 answers, you are getting input from the user as a str and not converting it to an int.
So it never hits the if condition and therefore your program doesn't end.
Please try this
numbers = []
out = 0
while out == 0:
numbers.append(int(input('Add a number: ')))
out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
print(type(out2))
if int(out2) == 1: # Convert the out2 to an int here
print(f" Inside if condition")
out = 1 # Use an assignment operator here
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
def show_hidden_word(secret_word, old_letters_guessed):
i = 0
new_string = ""
while i < len(secret_word):
j = 0
print(1)
for j in old_letters_guessed:
if secret_word[i] == old_letters_guessed[j]:
new_string += secret_word[i]
print(old_letters_guessed[j])
j += 1
print(secret_word[i])
i += 1
return new_string
Why the comparate between those string don't work?And can someone help to fix it?
This should help. You can convert the input to int or 9 to string '9'
choice = input('Enter an option - out of the loop')
while int(choice) != 9: #or while choice != '9'
menu(list_of_products, choice)
choice = input('Enter an option - in the loop')
Maybe choice is a string and you need to convert it to int.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
I'm starting out in python and just wrote a simple calculator but it seems to have some errors.Pls help me out
a = raw_input("Enter value of a : ")
b = raw_input("Enter value of b : ")
sum = a + b
sub = a - b
mul = a * b
div = a / b
print"1.Addition"
print"2.Subtraction"
print"3.Multiplication"
print"4.Division"
op = raw_input("Enter the operation to be done : ")
if op == 1:
print"Sum is %d" % sum
elif op == 2:
print"Difference is %d" % sub
elif op == 3:
print"Product is %d" % mul
elif op == 4:
print"Quotient is %d" % div
else:
print"Invalid operation"
Error is
TypeError : Unsupported operand type for -: 'str' and 'str'
You are reading strings, and trying them to subtract them as strings. You have to convert them to numbers first. Just add
a = float(a)
b = float(b)
after the user input
Moreover, sum is a builtin function in python, so you'd better use different names for your variables
change the input into an int by putting int() outside of raw_input.
a = int(raw_input("Enter value: "))
raw_input interprets the user input as strings therefore you need to convert the raw input into an int first before processing them