Lists function not working in if condition [duplicate] - python

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

Related

Why does this if statement not work correctly? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
This is supposed to be a random number guessing game. It is supposed to say whether the number was guessed correctly or not at the end but it will always display "Wrong" even if the number has been guessed correctly.
I've only been learning python for a couple of days and this is doing my head in.
import random
odds = [1,2]
rNum=(random.choice(odds))
print ('The odds are:',odds)
gNum=input('What is your guess?')
print ('You Guessed:',gNum)
print ('The number was:',rNum)
if (rNum == gNum):
(print("Correct!"))
if (rNum != gNum):
(print("Wrong"))
Its as if it can't compare if the number is the same or not and always interprets it as wrong, as it only will give the != result
Forgive me if I sound foolish I am only beginning
Many thanks :)
I imagine the types are different. One is an int, the other a str which happens to contain an integer. Try"
gNum=int(input('What is your guess?'))

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':

Triple nesting while loops Python 3.6.5 is not indefinite [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am trying to do triple nesting of while loops.
If you input a decimal number it returns error, then if you input number above 31 it returns error, but if you try again to input decimal number code stops. Need help making it indefinite loop no matter how many times, or what order, a user inputs incorrect format. Also need to verify that dates input are valid for number of days in given month?
import string
varD= input("Enter Date/Day:")
while varD.isdigit() or varD.isspace()\
or varD.isdecimal or int(varD)>31 \
or int(varD)==26 or int(varD)<=0:
print ("Error: Enter Valid Number!")
varD= input("Enter Day:")
else:
print ("You have entered:", varD)
Use an infinite loop and break only when all criteria are satisfied instead.
while True:
varD = input("Enter Day:")
if varD.isdigit() and not varD.isspace() and varD.isdecimal() \
and int(varD) < 32 and int(varD) != 26 and int(varD) > 0:
break
print("Error: Enter Valid Number!")
print("You have entered: %s" % varD)
Also, your understanding of the term triple nesting is incorrect. Triple nesting means something like this:
while expression1:
while expression2:
while expression3:
do_something()

Everything appears to be defined as a string? Python [duplicate]

This question already has answers here:
Specify input() type in Python?
(2 answers)
Closed 4 years ago.
I am completely new to programming. However, I just wanna write a simple bit of code on Python, that allows me to input any data and the type of the data is relayed or 'printed' back at me.
The current script I have is:
x = input()
print(x)
print(type(x))
However, regardless of i input a string, integer or float it will always print string? Any suggestions?
In Python input always returns a string.
If you want to consider it as an int you have to convert it.
num = int(input('Choose a number: '))
print(num, type(num))
If you aren't sure of the type you can do:
num = input('Choose a number: ')
try:
num = int(num)
except:
pass
print(num, type(num))
1111 or 1.10 are valid strings
If the user presses the "1" key four times and then Enter, there's no magic way to tell if they wanted to enter the number 1111 or the string "1111". The input function gives your program the arbitrary textual data entered by user as a string, and it's up to you to interpret it however you wish.
If you want different treatment for data in particular format (e.g. if they enter "1111" do something with it as a number 1111, and if they enter "111x" show a message "please enter a valid number") then your program needs to implement that logic.

Variable error in function [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 5 years ago.
I wanted to make a number guesser game, and I wrote this code:
from random import randint as rand
number=rand(-1,1001)
tries=0
def numguess():
guess=int(input("The chosen number is between 0 to 1000.\nEnter your guess : "))
tries=tries+1
numguess()
while True:
if number==guess:
print ("You won. My number was effectively" ,number,". \n It took you ",tries, "to guess the number.")
break
elif number<guess:
print ("The number I chose is lower than your guess")
numguess()
else:
print ("The number I chose is higher than your guess")
numguess()
When I run it, it asks me for input then raises a UnboundLocalError. Is there something I am doing wrong? I tried searching but I don't understand. Thank you.
your tries variable is treated locally - since you assign value to it, use:
global tries
tries = tries +1
in your function

Categories