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

时间:2019-04-01 23:12:54

标签: python python-3.x while-loop python-module

我正在制定价格正确的游戏。我目前正在与比赛行类似的游戏模式下工作,他们在其中猜测物品的价格。

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

  

“追踪(最近一次通话过去):         文件“ C:\ Users \ alexe \ AppData \ Local \ Programs \ Python \ Python36 \ Thepriceisright.py”,第36行,在           raceantrow()         第24行的文件“ C:\ Users \ alexe \ AppData \ Local \ Programs \ Python \ Python36 \ Thepriceisright.py”,在raceantrow中           protagnum = int(input(专有名称+“,您的出价是多少?”))       ValueError:以10为底的int()的无效文字:'alexei'“

这是我的代码:

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()

1 个答案:

答案 0 :(得分:0)

protagnum=int(input(propername +", what is your bid?"))

您正在将一个int /字符串转换为int。 “ 1”将起作用,但“ a”将引发ValueError

while True:
    try:
        protagnum=int(input(propername +", what is your bid?"))
        break
    except ValueError:
        print("Invalid bid, please try again")