Python issues if/elif [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 years ago.
I'm trying to make this code work but I can't seem to find the right solution.
while True:
tname = input("Please enter the unit of the temperature: ")
list=["Celsius","Kelvin","Fahrenheit"]
if tname == list[0] or list[1] or list[2]:
break
elif tname is not list[0] or list[1] or list[2]:
print("Not a valid unit. Please try again.")
I want the program to stop whenever either Celsius, Kelvin or Fahrenheit is typed but the program stops regardless of what I write. Do you guys know to fix it? Thanks in advance

The technically correct answer is that chaining comprehensions doesn't work like that; you have to do
if tname == list[0] or tname == list[1] or tname == list[2]:
But have you considered using in?
if tname in list:
or similarly:
if tname not in list:
Also, I'd advise against using list as the name of your list, as that's also the name of a type!

Related

How to check a given number is palindrome or not? [duplicate]

This question already has answers here:
Python Palindrome
(3 answers)
Closed 2 months ago.
I have tried reversing a given string and then checking it but want to know the actual method to solve such questions without using any built-in functions of python.
Tried using built-in functions in python but expecting to know the code without using them.
z=int(input("Enter number:"))
temp=z
rev=0
while(z>0):
dig=z%10
rev=rev*10+dig
z=z//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
You can use list slice for this problem
num = 1234
reverse = int(str(num)[::-1])
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
int(str(num[::-1]) is nothing but converting the string format to integer .

what code should I write to replace multiple "and" statements in one line? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
here is the code I am trying to fix:
the program runs but never returns covid negative even if I answer with a no, what would the correct syntax be to write this statement?
elif question1 and question2 and question3 and question4 and question5 and question6 == "NO":
print(name)
print(gender)
print(number)
print(adress)
print("COVID-19 Negative")
neg_counter = neg_counter + 1
Your code actually won't properly run at all – it'll compute as something like this:
(((question1 and question2) and question3) and question4)...
As in it's not actually calculating what I think you want, which is to see if any of them are =="NO".
One way to do this is:
elif "NO" in (question1, question2, question3, question4, question5, question6):
EDIT:
Since OP has clarified and wants to check if all of them are =="NO" or not, we can do this:
elif question1 == question2 == question3 == question4 == question5 == question6 == "NO":

Lists function not working in if condition [duplicate]

This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 2 years ago.
Hi I am new to programming and I like to try to make the code work different way.
Unfortunately, variable called unlucky cant be read when printing.
So when the user enter the listed value [7 6 5], it doesnt print out as "You have selected unlucky number". I have include the image and copy of my code :
option = int(input("Guess the correct number between 0 to 10 : \nYour choice :"))
unlucky_number= [7,6,5] # Unlucky numbers, listed
if option == unlucky_number: # should print somewhere close when user enters list number
print("You have selected a unlucky number")
elif option== 6: # only 6 is correct
print ("Correct Guess")
elif option in range (0,4):
print("Not close")
else:
print ("Not in the range, choose between 1 to 10")
Please tell me whats wrong with it and how can I make a better version of it.
Thank you enter image description here
if option == unlucky_number
This line is causing you troubles. Your "option" holds a singular number, but your "unlucky_number" is a list. List can not be equal to a number, they are completely different animals. What you want is:
if option in unlucky_number

Why does my if statement command not been functioning [duplicate]

This question already has answers here:
How to check variable against 2 possible values?
(6 answers)
Closed 4 years ago.
I am trying to program an ATM machine with this code in python. But regardles of what is inputted, it just says that the card is successfully inputed.
inputCard = input("Welcome to the atm machine, please insert your credit card (Type 'Yes' when you have done so) ")
if inputCard == ['No', 'no']: #checks if card has been entered
print ("Please retry")
else:
print ("Card is successfully inputed") `
Thanks
The equality operator == compares whether the input, which is a string, equals the right hand side, which is a list. Intuitively, a list will never equal a string.
So, use the in operator to see if the answer is in the possible options:
if inputCard in ('No', 'no'):
Alternatively, convert the answer to lowercase and then use ==:
if inputCard.lower() == 'no'
This way will accept no, No, NO and nO.
You are comparing the "inputCard" to a list. Try:
if inputCard.lower() == "no":
inputCard is str,["NO","no"] is list.They will not equal.And you can try like this
if inputCard.lower() == 'no':
or
if inputCard.upper() == 'NO':

How can I use while loop and if statement correctly in python? [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 8 years ago.
I have been having trouble with my python code and have been asking around for help. I have heard mixed things about this website but i have no where else to turn so i was hoping someone on here could show me the light and see if they can find out what's wrong with my code (other then it being messy and inefficient). here it is
D=False
while D==False:
input=raw_input("please type 1 for 10p, 2 for 20p, 3 for 50p, 4 for 1 pound")
while input not in ['1', '2', '3', '4']:
print "That is not a correct coin value"
input = raw_input("input: ")
else:
if input=="1":
m=m+10
if input=="2":
m=m+20
if input=="3":
m=m+50
if input=="4":
m=m+100
print("your current credit in pennys is:")
print(m)
D2=raw_input("are you done")
if D2=="yes" or "Yes":
D=True
else:
D=False
I kind of mucked up the implantation of the code on here but you get the picture. It all works fine until you get to the bit asking you if you are done. For some reason it carries on even if you don't put yes. Can any nice coders out there tell me what's up with it
This
if D2=="yes" or "Yes":
always evaluates to true, as it's essentially parsed like this:
if (D2=="yes") or "Yes":
You probably wanted:
if D2 == "yes" or D2 == "Yes":
Which is better written as:
if D2.lower() == "yes":
Or:
if D2 in ["yes", "Yes"]:

Categories