This question already has answers here:
How can I read inputs as numbers?
(10 answers)
How can I check if a string represents an int, without using try/except?
(23 answers)
Closed 4 years ago.
I've written a number guessing game in Python 2.7.14 and encountered something odd:
I ask for an integer as input and check for that, but the name of the variable (here "a") is always accepted, although no other strings or characters are accepted. Isn't this an issue when the user can enter variable names even though they are not allowed?
My code is:
from random import randint
a = randint(0,10)
b = input("enter a number between 0 and 10 ")
print "you entered :", b
if type(b) != int:
print "please enter only integers"
else:
if b < a:
print "b is smaller than a"
elif b == a:
print "b is equal to a"
else:
print "b is NOT smaller than a"
print "number was", a
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I am creating code for a simple teachers grading system in which you input the numeric grade and it tells you if it is an A, B, C, etc.
I first wanted to put a limit on the numbers you can input, i.e. can't put less than zero or more than one hundred, but it is not working. Can anyone explain why?
import time
from time import sleep
grade = 0
maximum = 100
minimum = 0
grade = input("""Insert numeric grade:
""")
time.sleep(1)
if grade > maximum:
print ("Please enter a valid grade.")
Basically you should fix this line:
if int(grade) > maximum:
because the result of input is a string, not a number, so the wrong comparison is used.
Of course, you could (and should!) also properly check that user has entered an integer value, otherwise there will be a ValueError.
try:
if int(grade) > maximum:
print("enter a value less than maximum")
except ValueError:
print("enter an integer value")
This question already has answers here:
Comparing a string to multiple items in Python [duplicate]
(3 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 2 years ago.
Hi I'm new to python and I'm having some trouble with logical operators. In the code I want the user to input one of three choices A, S , or D and reject anything else. The problem Im having is that when I input A, S, or D it still prints out Invalid Input.
guess = input("Lower (a), Same (s), Higher (d): ")
if guess != "a" or "s" or "d":
print ("Invalid Input")
Im using python version 2.7 if that helps
Here or short circuit operator compares the two True values + the condition guess != 'a' (Because non empty values evaluate to True in Python), so use not in:
guess = input("Lower (a), Same (s), Higher (d): ")
if guess not in ['a','s','d']:
print("Invalid Input")
This question already has answers here:
Reading in integer from stdin in Python
(4 answers)
Closed 4 years ago.
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers = []
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers = []
loop = int(loop)
...
This question already has answers here:
Comparing numbers give the wrong result in Python
(4 answers)
Closed 4 years ago.
Very new to programming and decided to get into Python. Sorry in advance if this question is very simple. Experimenting with functions. Got this to run with no errors, but doesn't always work.
I'm pretty sure I'm doing the string replacement wrong? Any explanation or advice would be great.
Noticed it wasn't working when I was comparing a single digit number to a multi digit.
def bigger(a, b):
if a > b:
print ("%s is bigger" % a)
elif a == b:
print ("They are both equal!")
else:
print ("%s is bigger" % b)
a = input("Pick a number: ")
b = input("Pick another number: ")
bigger(a, b)
input by default return String. You need to convert it to numbers. Change the int into float or double, if dealing with decimal numbers
def bigger(a, b):
if int(a) > int(b):
print ("%s is bigger" % a)
elif a == b:
print ("They are both equal!")
else:
print ("%s is bigger" % b)
a = input("Pick a number: ")
b = input("Pick another number: ")
bigger(a, b)
a = input("Pick a number: ")
b = input("Pick another number: ")
input() is always represented as a string and not an integer so in order to run comparisons like if a > b you must first convert it to an integer, like this:
a = int(input("Pick a number: "))
b = int(input("Pick another number: "))
The reason your program works is because Python is comparing the individual sizes of your strings. So "32" starts with a 3 while "122" starts with a 1 so 32 is larger.
>>> '122'<'32'
True
>>> '44'>'1333333'
True
This question already has answers here:
SyntaxError invalid token
(5 answers)
Closed 5 years ago.
With number = 001, the error is 'invalid token'.. Please explain why and why 1 and 001 is not treated the same way by the compiler?
number = 001
def palindrome(number):
print ("The number is: ",number)
str1 = str(number)
strrev = str1[::-1]
if (str1 == strrev):
return True
else:
a = int(str1)
b = int(strrev)
c = a+b
print ("Sum with reverse: ",c)
print (" ")
return (palindrome(c))
n = palindrome(number)
print ("Palindrome: ",n)
001 is an integer it will work as 1, instead of 001. If you need 001 use it as string not an integer. As 001 is invalid integer, but when it comes to string variable it works fine.
Use :
stringNumber = '001'