首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误提示而不是提示重新输入输入

错误提示而不是提示重新输入输入
EN

Stack Overflow用户
提问于 2019-04-01 23:12:54
回答 1查看 78关注 0票数 1

我正在制定一个价格是正确的游戏。我目前正在进行一种类似于参赛者排的游戏模式,他们在那里猜测一件物品的价格。

当它要求您提交一个出价时,如果您输入一个单词(而不是bid),程序就会崩溃,并显示以下错误:

“回溯(最近一次调用):文件"C:\Users\alexe\AppData\Local\Programs\Python\Python36\Thepriceisright.py",第36行,contestantrow() "C:\Users\alexe\AppData\Local\Programs\Python\Python36\Thepriceisright.py",第24行,contestantrow protagnum=int(输入(propername+,您的出价是多少?”) ValueError:带基数10:‘alexei’的无效文字int()

这是我的密码:

代码语言:javascript
复制
import random
print(" The Price is Sorta Right - 000776331")
welcomeplayer = True
contestantrow = True
def welcome():
    while True:
        global welcomeplayer
        global propername
        welcomeplayer =  input("Please enter your name using only letters")
        validname = welcomeplayer.isalpha()
        propername = welcomeplayer.capitalize()
        if validname == True:
            print( propername, " ! Come on down! You're the next contestant on the Price is (sorta) right")
            print (" Dew Drop welcomes " ,propername ," to contestants row joining EIMNOT A. HUMAN,ARTHURFICIAL EINTEL , ROBORT")
            return
        else:
            print("Please only write letters on your name tag")
            welcomeplayer = False

def contestantrow():
    while True:

        print("Dew Drop shows the price that you are bidding on")
        protagnum=int(input(propername +", what is your bid?"))
        if protagnum > 0:
            componebid = random.randint(1,1000)
            print("EIMNOT A. HUMAN bids: ",componebid)
            comptwobid = random.randint(1,1000)
            print("ARTHURFICIAL EINTEL bids: ",comptwobid)
            compthreebid =random.randint(1,1000)
            print("ROBORT bids: ",compthreebid)
        else:
            print(" Dew Drop says [Im sorry bids should start at atleast one dollar]")
            contestantrow = False
welcome()
contestantrow()
EN

回答 1

Stack Overflow用户

发布于 2019-04-01 23:21:35

代码语言:javascript
复制
protagnum=int(input(propername +", what is your bid?"))

您正在将int / string转换为int。"1“将起作用,但"a”将引发ValueError

代码语言:javascript
复制
while True:
    try:
        protagnum=int(input(propername +", what is your bid?"))
        break
    except ValueError:
        print("Invalid bid, please try again")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55464821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档