|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我按照视频编写了一段代码,感觉虽然跟视频里的效果差不多,但是有一个小毛病我想解决掉。 设置输入不同的数字时,会显示相应的语句。 但是我如果不输入数字,而是直接打回车,就会出现一大串字符,这个要怎么解决?
代码如下:
print("mulai")
temp = input("math")
yuan = int(temp)
import random
ai = random.randint(0,100)
while yuan != ai:
print("heart is busy")
temp = input("why")
yuan = int(temp)
if yuan >= ai:
print("what")
if yuan <= ai:
print("where")
print("forever")
直接回车的错误反馈如下:
Traceback (most recent call last):
File "D:\Python\Python38\1.py", line 3, in <module>
yuan = int(temp)
ValueError: invalid literal for int() with base 10: ''
本帖最后由 Jamesonwjt 于 2020-9-3 23:01 编辑
print("mulai")
temp = input("math")
while True:
try:
temp != '\n'
yuan = int(temp)
break
except:
temp = input("math")
import random
ai = random.randint(0,100)
print(ai)
while yuan != ai:
print("heart is busy")
temp = input("why")
yuan = int(temp)
if yuan >= ai:
print("what")
if yuan <= ai:
print("where")
print("forever")
我更新了一下我的代码,你可以再试试看,忘记加break
|
|