【问题标题】:Make the user choose whether or not they want to restart the game让用户选择是否要重启游戏
【发布时间】:2020-08-23 07:14:18
【问题描述】:

代码开始,我让用户输入他想猜的数字范围...如果他猜的数字低于随机选择的数字,它会打印再次尝试并猜高,反之亦然。

import random
import time
print("This is a guessing game!")        
print("")
user=input("Start by entering your name\n")
print("Hello "+user+"!")
print("Choose the end of the range by entering a number\n(Starting by default on 1)")
cho=input()
while  (float(cho))<1:
    print("Please enter another number , you have mistyped something")
    cho=input()
    if cho== range (1,9):
        continue
print("I am currently thinking a number from 1 to "+cho)
num=random.randint((float(1)),(float(cho)))
guess=input("Take a guess!\n")
while ((float(guess)) > (float(num))):
    print("Your number is higher than the one i thought.Guess lower")
    guess=float(input())
while float(guess) < float(num):
    print("Your number is lower than the one i thought.Guess higher")
    guess=float(input())
if float(guess)==float(num):
    print("You guessed right!Good Job!")
    print("The number was "+str(num))
    print("Thanks for playing!")
    time.sleep(2)
    print("Bye")
    time.sleep(1 )
    exit()

用户在这里完成后,我想要一个 if 和一个 elif 以便他可以选择是否要重新启动“游戏”

【问题讨论】:

  • 你的问题不是很清楚。您可能需要对其进行编辑并使其更清晰。
  • 现在怎么样? (对不起,我是这个网站的新手)
  • 清晰一点​​。仍然有点难以弄清楚如何提供帮助。如果你想知道,我不是那个投反对票的人。
  • 您能否更具体地说明问题所在?请参阅How to Askhelp center

标签: python


【解决方案1】:

你可以有一个输入语句询问“你想继续玩还是退出?”

这将是你的全部代码:

import random
import time
loop = 5
while loop == 5:
    print("This is a guessing game!")        
    print("")
    user=input("Start by entering your name\n")
    print("Hello "+user+"!")
    print("Choose the end of the range by entering a number\n(Starting by default on 1)")
    cho=input()
    while  (float(cho))<1:
        print("Please enter another number , you have mistyped something")
        cho=input()
        if cho== range (1,9):
            continue
    print("I am currently thinking a number from 1 to "+cho)
    num=random.randint((float(1)),(float(cho)))
    guess=input("Take a guess!\n")
    while ((float(guess)) > (float(num))):
        print("Your number is higher than the one i thought.Guess lower")
        guess=float(input())
    while float(guess) < float(num):
        print("Your number is lower than the one i thought.Guess higher")
        guess=float(input())
    if float(guess)==float(num):
        print("You guessed right!Good Job!")
        print("The number was "+str(num))
        keep = input("Do you want to keep playing? ")
        if keep == "Yes" or keep == "yes":
            loop = 5
        elif keep == "No" or keep == "no":
            print("Thanks for playing!")
            loop = 4

希望这就是你想要的!

【讨论】:

  • 我只是添加一个 else keep !=.... 以防用户输入错误
【解决方案2】:

这是您的代码的一个更简洁的版本。我还认为使用整数可能更有意义,并且只转换一次。

import random

print("This is a guessing game!\n")
user = input("Start by entering your name\n")

running = True
while running:
    print("Hello "+user+"!")
    print("Choose the end of the range by entering a number\n(Starting by default on 1)")
    cho=input()
    while  (float(cho))<1:
        print("Please enter another number , you have mistyped something")
        cho=input()
    print("I am currently thinking a number from 1 to "+cho)
    num = random.randint(1, int(cho))
    guess = int(input("Take a guess!\n"))
    while guess > num:
        guess = int(input("Your number is higher than the one i thought. Guess lower"))
    while guess < num:
        guess= int(input("Your number is lower than the one i thought. Guess higher"))
    if guess == num:
        print("You guessed right! Good Job!")
        print("The number was " + str(num))
        keep_playing = input("Do you want to keep playing? ")
        if keep_playing == "No" or keep_playing == "no":
            print("Thanks for playing!")
            running = False

【讨论】:

  • 谢谢,我会将它与其他答案结合起来,这将是完美的!
猜你喜欢
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多