Just starting to learn a bit of python, and for some reason, my script I just put together returns that a number such as 10000000000000000 < 5. I assume this is due to native inaccuracy with the int
type at large values, but I'm not sure, perhaps I'm just doing something wrong!
Here's my (poorly written, I know) script:
def checkValue(n):
while True:
if n == '':
print 'You didn\'t enter anything!'
return False
else:
try:
n = int(n)
except ValueError:
print 'That is not an integer!'
return False
else:
break
return True
while True:
firstNum = raw_input('Enter the first number: ')
if checkValue(firstNum) == False:
continue
else:
break
while True:
secNum = raw_input('Enter the second number: ')
if checkValue(secNum) == False:
continue
else:
break
while True:
thirdNum = raw_input('Enter the third number: ')
if checkValue(thirdNum) == False:
continue
else:
break
if thirdNum > secNum and thirdNum > firstNum:
print 'The third number is the biggest.'
elif secNum > firstNum:
print 'The second number is the biggest.'
else:
print 'The first number is the biggest.'
In your " checkValue
" function you are converting the inputs in to " int
". But at the comparison you use entered string values. So you can convert "firstNum", "secNum" and "thirdNum" at the input stage. See the difference.
In [2]: firstNum = raw_input('Enter the first number: ')
In [3]: firstNum
Out[3]: '5'
In [4]: int_first = int(firstNum)
In [5]: int_first
Out[5]: 5
您需要使用intfirstNum = int(firstNum)
将原始输入, firstNum
等转换为整数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.