【问题标题】:my function isn't returning anything in python我的函数在 python 中没有返回任何内容
【发布时间】:2013-03-04 02:46:42
【问题描述】:

我为我在计算机科学课上工作的游戏做了一个函数定义。

 def bidf(bid): #this card is going to repeat until they give a valid bid
     bid=float(input("What is your bid? "))
     if bid>pot:
         print("Invalid bid: You can't bid more than what is in the pot")
         bidf()
     elif bid>value:
         print("Invalid bid: You don't have that much")
         bidf()
     elif bid<0:
         print("You can not bid negative money")
         bidf()
     else:
         return float(bid)

每次我尝试使用出价变量时它都不起作用 例如,这段代码总是崩溃的地方

if g=="y":
      bidf()
      if card(insidecard[0])==card(outsidecard[0]):
           z=input("Will the next card be higher or lower than the pair?(h/l): ")
           if card(insidecard[0])==14 and card(hand[0])==14:
                print("You quadruple your bid and lose $",bid)
                bid=bid*4
                value=value-bid
                pot=pot+bid

我想要做的是让出价出现在打印语句中,以及用于对其他变量进行数学运算的出价。

【问题讨论】:

    标签: python function if-statement printing nested


    【解决方案1】:

    将您对bidf() 的所有呼叫替换为return bidf()

    【讨论】:

    • 谢谢,这有帮助。不知道现在我在想什么。
    【解决方案2】:

    当您在代码中调用bidf() 时,您需要存储返回值。因此,请执行以下操作:

    if g=="y":
        bid = bidf()
    

    【讨论】:

      猜你喜欢
      • 2020-11-22
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 2015-10-09
      • 2016-07-24
      • 1970-01-01
      相关资源
      最近更新 更多