
[英]Invalid syntax when using “%” sign on print function on python 3.4.3
[英]Invalid token when using 001 in Python 3.4.3
我在网上寻找解决方案,但没有找到任何相关的解决方案。 我的课程作业要求我制作一个程序,计算机会尝试猜测用户输入的数字。 数量需要介于001和100之间,所以我首先尝试确保用户只能输入该范围内的数字。
我到目前为止的代码:
import random
code=(int(input("Input a three digit code. Must be more than 001 and less than 100.")))
print(code)
if (code < 001) or (code > 100):
print ("Invalid code")
elif (code > 001) or (code < 100):
print ("Valid code")
它工作正常,001改为1,但如果我用001运行它我得到一个无效的令牌错误。 这需要在Python 3.4.3中完成。 任何形式的帮助将不胜感激。
整数在Python中不能有前导零。 之前的前导零(Python 2)用于表示八进制数字。 由于许多人不知道并且为什么070 == 56
而感到困惑,因此Python 3使得前导零非法。
您不应该将code
转换为整数 - 仅存储数值变量中的实际数字(您打算进行计算)。 保持字符串:
while True:
code = input("Input a three digit code. Must be more than 001 and less than 100.")
try:
value = int(code)
except ValueError:
print("Invalid code")
continue
if 1 <= value <= 100:
print ("Valid code")
break
else:
print ("Invalid code")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.