Python中处理负整数的问题

我正在尝试检查一些条件的输入,然后将字符串转换为整数,在此之后我想确保整数不是负数,否则提示用户再次输入。


它适用于字符串条件,但是当我输入负数时,输入会引发错误“输入预计最多 1 个参数,得到 2”


关于如何评估这一点的任何想法?


     #This compares whether the bet placed is greater than the value in the players chip_balance. It prompts the player for a lower bet if it is of greater value than chip_balance

 while bet > chip_balance:

      print('Sorry, you may only bet what you have 0 -', chip_balance)

      bet = input("Place your bet: ")

      while bet == '':

          bet = input("Can't be an empty bet please try again ")

      while bet.isalpha() or bet == '':

          bet = input("Must be a numerical value entered \n \n Place You're bet:")


      bet = int(bet)

 if bet < 0:

      bet = input("Sorry, you may only bet what you have sir! 0 \-", chip_balance)


      bet = int(bet)


米琪卡哇伊
浏览 213回答 2
2回答

拉丁的传说

bet&nbsp;=&nbsp;input("Sorry,&nbsp;you&nbsp;may&nbsp;only&nbsp;bet&nbsp;what&nbsp;you&nbsp;have&nbsp;sir!&nbsp;0&nbsp;\-",&nbsp;chip_balance)input 函数不接受 2 个参数,而 print 接受。你可以这样做;bet&nbsp;=&nbsp;input("Sorry,&nbsp;you&nbsp;may&nbsp;only&nbsp;bet&nbsp;what&nbsp;you&nbsp;have&nbsp;sir!&nbsp;0&nbsp;\-&nbsp;{}".format(chip_balance))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python