This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
How to use user input to end a program?
(6 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 22 days ago.
Here's the beginning of my program:
import sys
print("Enter a number when prompted, type \"stop\" to stop.")
x = input("Input: ")
if type(x) == "int":
Higher = x
Lower = x
elif x.lower() == "stop":
print("The program has been stopped.")
sys.exit()
If right after this I try to print the variable Higher, for example, I get this error message: NameError: name 'Higher' is not defined
EDIT: I tried declaring the variables beforehand but that doesn't work either.
Related
This question already has answers here:
Determine whether integer is between two other integers
(16 answers)
Closed last month.
if money == (1, 799999):
print("You're a natural!)
I need to make it so, if the money variable is = to anything in between 1 and 799999 it can return a print. But i cant find anything to do it
You can use the following code:
if money>=1 and money<=799999:
print("You are natural.")
Hope it works.....
This question already has answers here:
How do you input integers using input in Python [duplicate]
(3 answers)
Closed 2 years ago.
whats my mistake?
it doesn't show you are right
print("what's the correct number?")
print("1/2/3/4/5/6/7/8/9")
correctNumber = 6
inputNumber = input()
if inputNumber == correctNumber:
print("you are right")
You can try
correctNumber = 6
inputNumber = int(input("what's the correct number? 1/2/3/4/5/6/7/8/9"))
if inputNumber == correctNumber:
print("you are right")
The problem was that input is store in a variable as a string and you need to cast it to int to compare it with int since correctNumber = 6 is int.
By the way, the input method expects a string to print the user before his input so you don't need to use print statements to notify the user about the expected input.
When using input(), you get a string that you compare int () and str (). to get a number in water you need int(input())
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Check if multiple strings exist in another string
(17 answers)
Closed 3 years ago.
Trying to accept only text as input in my program, this is a snippet where I'm having trouble.
def askStr(th):
try:
a = str(input(th))
except ValueError:
raise ValueError("You may only enter a string (letters.)")
if "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" in a:
raise TypeError("No. Only enter letters.")
return a
When I enter a number, it raises the error as expected. BUT, when I enter anything else, only letters, it still gives me the error. No idea what to do here.
Try this
def askStr(th):
a = str(input(th))
for c in a:
if c in string.digits:
raise TypeError("No. Only enter letters.")
return a
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I created tuples as keys in dictionary and trying to run a search with user input.
directory = {('a','b'):1, ('c','d'):2, ('e','f'):3}
numsearch = input("enter number to search: ")
if numsearch in directory.values():
print(directory.values())
or
print(directory.items())
does not throw any error or output a value.
Appreciate any help if you tell me the error in my code
directory = {('a','b'):1, ('c','d'):2, ('e','f'):3}
numsearch = input("enter number to search: ")
for k,v in directory.items():
if int(numsearch) == v:
print(k)
This question already has answers here:
Convert int to ASCII and back in Python
(6 answers)
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I have something like this right now but this wont work because chr requires an integer. How can I make it print the chr of the user input?
num1 = input("Enter a ASCII code 0 - 127:")
print(str(chr(num1)))
Just cast the user input to int first -
num1 = input("Enter a ASCII code 0 - 127:")
print chr(int(num1))
(Incorporated #chepner's comment)