【问题标题】:Proper way of writing recurring function in python在python中编写循环函数的正确方法
【发布时间】:2020-09-05 20:05:06
【问题描述】:

所以我是 python 新手,我尝试制作一个随机数游戏生成器作为练习。您需要在有限的尝试次数内猜测随机数。这是我的代码。

import random

rand_num  = random.randint(1,6)

print("\nNUMBER GUESSING GAME")
lives = 5

def guess():
    guess = int(input("Input guess: "))
    return guess

print(rand_num) #to see the correct answer

x = guess()
if x == rand_num:
    print("Good job! YOU WIN.\n")

else:
    while x != rand_num:
        
        if x == rand_num:
            print("Good job! YOU WIN.\n")
            break

        lives -= 1
        print("Incorrect. Please try again")
        guess()

        if lives == 0:
            print("YOU LOSE.\n")
            break

因此,如果您的第一次猜测是错误的,而您在随后的尝试中找到了正确答案,那么游戏将无法识别它。

提前致谢。

【问题讨论】:

    标签: python-3.x function recursion


    【解决方案1】:

    你在正确的轨道上。以后,让自己通过程序思考每一行在做什么以及顺序是否合乎逻辑。

    您的代码中只有一个实际错误:在 while 循环中调用 guess 时,您没有在代码中收集新的 x 值。其他元素是正确的,只是乱序而已。这是一个更合乎逻辑的顺序,解决了收集问题,

    import random
    
    rand_num  = random.randint(1,6)
    
    print("\nNUMBER GUESSING GAME")
    lives = 5
    
    def guess():
        guess = int(input("Input guess: "))
        return guess
    
    print(rand_num) #to see the correct answer
    
    x = guess()
    if x == rand_num:
        print("Good job! YOU WIN.\n")
    else:
        while x != rand_num:
            # 1) Notify the user
            print("Incorrect. Please try again")
            # 2) Decrement remaining guesses
            lives -= 1
            # 3) Check if maximum number of guesses
            # reached - end if yes
            if lives == 0:
                print("YOU LOSE.\n")
                break
            # 4) Get a new guess 
            x = guess()
            # 5) Compare            
            if x == rand_num:
                print("Good job! YOU WIN.\n")
    

    您可能想通知您的玩家要猜的数字范围是多少。您还可以扩展此练习,编写多个难度级别,每个级别都有不同的范围(例如简单级别 1-6、中等级别 1-20 等),供玩家选择。


    正如@Stef 在评论中提到的那样,将猜测数(生命)条件作为 while 循环条件的一部分在技术上更简洁且更常见,

    import random
    
    rand_num  = random.randint(1,6)
    
    print("\nNUMBER GUESSING GAME")
    lives = 5
    
    def guess():
        guess = int(input("Input guess: "))
        return guess
    
    print(rand_num) #to see the correct answer
    
    x = guess()
    if x == rand_num:
        print("Good job! YOU WIN.\n")
    else:
        while (x != rand_num) and (lives > 0):
            # 1) Notify the user
            print("Incorrect. Please try again")
            # 2) Decrement remaining guesses
            lives -= 1
            # 3) Get a new guess 
            x = guess()
            # 4) Compare, enf if correct             
            if x == rand_num:
                print("Good job! YOU WIN.\n")
    
    # If ending because no more guesses left 
    if lives == 0:
        print("YOU LOSE.\n")
    

    我对每个条件都使用括号,这里没有必要和样式问题。我喜欢这样分隔条件。

    此外,如果所有猜测都用尽了,"YOU LOSE.\n" 部分现在会打印在末尾。

    请注意:使用您现在的代码,您的玩家实际上可以猜测 6 次,在开始时猜测一次,在 while 循环中再猜测 5 次。

    【讨论】:

    • 我会写 while x!= rand_num and lives > 0 因为有一个循环条件然后在不同的条件下中断是欺骗性的,并且更难理解 lives 的用途。
    • @Stef 我同意,只是想坚持原来的,但我现在会添加它作为更好的选择。
    • 非常感谢大家,现在可以正常使用了。每当我学习新技术时,我一定会升级它并添加一些东西。级别绝对是一个,如果输入是 int 也是一个检查器,等等
    • 你可以取消第二个if x == rand_num:的缩进,这样它就可以在循环之外进行测试(实际上你可以将它与if lives==0结合起来,所以它是一个if / else
    猜你喜欢
    • 2020-04-24
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多