Comparison between string cell to list cell in python [duplicate] - python

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.

Related

Python if statement gone wrong. Whats wrong? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
I don't understand what's wrong with my code here. Can someone help me pls? I've been trying to solve it all morning.
question = input("Choose from 0 to 1 : ")
mylist = ["Mark", "Jenny"]
if question == 0:
print(mylist[0], "is your new friend")
elif question == 1:
print(mylist[1], "is your new friend")
else:
print("I said choose from 0 to 1")
The problem is in the data types:
input() returns a string but in your, if statement you're comparing a string "0" to an integer 0. Because of that else is always executed.
Concert the input() into int() like shown below:
question = int(input("Choose from 0 to 1 : "))
mylist = ["Mark", "Jenny"]
if question == 0:
print(mylist[0], "is your new friend")
elif question == 1:
print(mylist[1], "is your new friend")
else:
print("I said choose from 0 to 1")

Python composed Lists [duplicate]

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)

How to write in superscript in Python 3? [duplicate]

This question already has answers here:
How do you print superscript in Python?
(13 answers)
Closed 1 year ago.
I am writing a code which is supposed to print a series in the form:
x - (x^2)/2! + (x^3)/3! - (x^4)/4! ... (x^n)/n!
where x and n are input from the user end.
For now my code looks like this
x = int(input('Enter number: '))
n = int(input('Till which term? '))
#for series
for i in range(1,n+1,1):
if i == n:
print('('+str(x)+'^'+str(i)+')'+'/'+str(i)+'!')
elif i == 1:
print(str(x),end = ' - ')
else:
if i%2 != 0:
print('('+str(x)+'^'+str(i)+')'+'/'+str(i)+'!',end = ' - ')
else:
print('('+str(x)+'^'+str(i)+')'+'/'+str(i)+'!',end = ' + ')
but the output is:
================= RESTART: E:/Python/Files/special series 4.py =================
Enter number: 3
Till which term? 6
3 - (3^2)/2! + (3^3)/3! - (3^4)/4! + (3^5)/5! - (3^6)/6!
is there any function to write in superscript to avoid the arrow-head sign?
As the output of Python and pretty much any language is usually in unicode or ASCII, the answer is simply no.

Why is .append not adding string to the list? [duplicate]

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("-->"))

i don't know what is wrong with my coding it keeps skip to else statement even i type in integers not float [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
def factorial(n):
if isinstance(n,int):
if n == 1:
return 1;
elif n <= 0:
print("Factorial is for positive integer.")
else:
return n*factorial(n-1)
else:
print("It's only for integers")
factorial_number = input("give an integer that you want to factor: ")
print(factorial(factorial_number))
You can handle is as soon as you get the input, see below example:
if factorial_number.isdigit():
factorial_number = int(factorial_number)
else:
print("It's only for integers")
The built-in input() always return a str object. You need to cast it to int.
factorial_number = int(input("give an integer that you want to factor: "))
print(factorial(factorial_number))

Categories

Resources