有人可以帮我重新设计一下这段代码,以便程序提示用户选择Easy,Medium或Hard。
Easy: maxNumber = 10
Medium: maxNumber = 50
Hard: maxNumber = 100
它应该选择0和maxNumber之间的随机数。 该程序将循环调用一个函数得到用户猜测,另一个来检查他们的猜测。一个名为“getGuess”的函数,它将询问用户他们的猜测和重新提示 如果猜测不在0和maxNumber之间 r函数名为“checkGuess”,它将检查用户的猜测 比较答案。 如果数字较高,该函数将返回“更高” 而不是猜测;如果数字低于猜测,则“降低”;如果数字等于猜测,则“正确”。 一旦用户正确猜出了数字,程序将显示他们所有的猜测和 他们花了多少猜测。然后程序会询问用户是否愿意尝试 再次重新显示难度菜单。
import random
guessesTaken = 0
print('Hello! Welcome to the guessing game')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
答案 0 :(得分:-1)
你可以这样做:
from random import randint
myName = input("what's your name? ")
def pre_game():
difficulty = input("Choose difficulty: type easy medium or hard: ")
main_loop(difficulty)
def main_loop(difficulty):
if difficulty == "easy":
answer = randint(0, 10)
elif difficulty == "medium":
answer = randint(0, 50)
else:
answer = randint(0, 100)
times_guessed = 0
guess = int()
while times_guessed < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
times_guessed += 1
if guess < answer:
print('Your guess is too low.')
if guess > answer:
print('Your guess is too high.')
if guess == answer:
break
if guess == answer:
guessesTaken = str(times_guessed)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != answer:
print('Nope. The number I was thinking of was ' + str(answer))
next = input("Play again? y/n: ")
if next == "y":
pre_game()
else:
print("Thanks for playing!")
pre_game()