0

我正在尝试使用我的第一种脚本语言:python。我目前正在制作一个小游戏,您可以在其中猜测 1-100 的预定数字。它还会告诉您正确答案是更大还是更小。目前我的代码总是返回正确的数字较小,而事实并非如此。

编辑:非常感谢你帮助我!

import random

print("this is a game made by Sven")
print("you're supposed to guess the right number. it is between 0 & 101.")
print("lucky for you, you're getting hints.")
print("succes!")

rightnumber = (random.randint(1, 100))
print(rightnumber)

gok = int(input("type your guess here and press enter"))

def compareguess():
    if gok > rightnumber:
        print("wrong! the right number is smaller :)")
    elif gok == rightnumber:
        print("WOW! you guessed right!")
    else:
        print("wrong! the right number is bigger.")
compareguess()

def guessAgain():
    int(input("try again. type your guess and press enter."))
    compareguess()
    if rightnumber == gok:
        print("that was it! you won!")
    else: guessAgain()


if rightnumber == gok:
    print("that was it! you won!")
else: guessAgain()
4

5 回答 5

3

您可以gok作为参数传递以避免全局变量。

问题是:

当您再次输入时:int(input("try again. type your guess and press enter.")),您不会将值分配给任何东西。所以gok保持不变,总是更大或更小

import random

print("this is a game made by Sven")
print("you're supposed to guess the right number. it is between 0 & 101.")
print("lucky for you, you're getting hints.")
print("succes!")

rightnumber = (random.randint(1, 100))

gok = int(input("type your guess here and press enter"))

def compareguess(gok):
    if gok > rightnumber:
        print("wrong! the right number is smaller :)")
    elif gok == rightnumber:
        print("WOW! you guessed right!")
    else:
        print("wrong! the right number is bigger.")
compareguess(gok)

def guessAgain():
    gok=int(input("try again. type your guess and press enter."))
    compareguess(gok)
    if rightnumber == gok:
        print("that was it! you won!")
    else: guessAgain()


if rightnumber == gok:
    print("that was it! you won!")
else: guessAgain()

另外,不要使用递归。相反,使用while循环。没有递归的缩短代码:

import random

print("this is a game made by Sven")
print("you're supposed to guess the right number. it is between 0 & 101.")
print("lucky for you, you're getting hints.")
print("succes!")

rightnumber = (random.randint(1, 100))
while True:
    gok = int(input("type your guess here and press enter"))
    if gok > rightnumber:
        print("wrong! the right number is smaller :)")
    elif gok == rightnumber:
        print("WOW! you guessed right!")
        break
    else:
        print("wrong! the right number is bigger.")
于 2021-08-01T12:16:40.927 回答
1

试试这个代码

import random

print("this is a game made by Sven")
print("you're supposed to guess the right number. it is between 0 & 101.")
print("lucky for you, you're getting hints.")
print("succes!")

rightnumber = (random.randint(1, 100))
print(rightnumber)

gok = int(input("type your guess here and press enter"))

def compareguess(gok):
    if gok == rightnumber:
        print("WOW! you guessed right!")
    elif gok > rightnumber: 
        print("wrong! the right number is smaller :)")
    else:
        print("wrong! the right number is bigger.")
compareguess(gok)

def guessAgain():
    gok = int(input("try again. type your guess and press enter."))
    compareguess(gok)
    if rightnumber == gok:
        print("that was it! you won!")
    else: guessAgain()


if rightnumber == gok:
    print("that was it! you won!")
else: guessAgain()
于 2021-08-01T12:11:18.140 回答
1

问题在于参数的范围。第一个猜测全局存储在gok. 无论是小还是大,compareguess都会打印出正确的信息。但是当你打电话时,guessAgain你不会在任何地方保存新的猜测,所以值gok仍然是旧的。即使您要保存它,范围也不会正确,因此您不会在compareguess.

所以一个可能的解决方案是gok作为参数传递给compareguess(我也rightnumber作为参数添加)。

def compareguess(gok, rightnumber):
    if gok > rightnumber:
        print("wrong! the right number is smaller :)")
    elif gok == rightnumber:
        print("WOW! you guessed right!")
    else:
        print("wrong! the right number is bigger.")

现在会运行 -

while rightnumber != gok:
     gok=int(input("try again. type your guess and press enter."))
     compareguess(gok, rightnumber)
于 2021-08-01T12:14:53.210 回答
0

你可以用这个。它会一直询问,直到您输入正确的数字。

import random

print("this is a game made by Sven")
print("you're supposed to guess the right number. it is between 0 & 101.")
print("lucky for you, you're getting hints.")
print("succes!")

rightnumber = random.randint(1, 100)
print(rightnumber)

def guess():
    gok = int(input("type your guess here and press enter: "))

    if gok > rightnumber:
        print("wrong! the right number is smaller :)")
        guess()
    
    elif gok == rightnumber:
        print("WOW! you guessed right!")
        print("that was it! you won!")
    
    else:
        print("wrong! the right number is bigger.")    
        guess()

guess()
于 2021-08-01T12:26:27.937 回答
0

你的compareguess功能

def compareguess():
    if gok < rightnumber:
        print("wrong! the right number is smaller :) ")
    elif gok == rightnumber:
        print("WOW! you guessed right!")
    else:
        print("wrong! the right number is bigger. ")

你的guessAgain功能

def guessAgain():
    global gok
    gok = int(input("try again. type your guess and press enter."))
    compareguess()
    if rightnumber == gok:
        print("that was it! you won!")
    else:
        guessAgain()
于 2021-08-01T12:15:04.167 回答