python - 让用户选择是否要重启游戏
问题描述
代码开始,我让用户输入他想猜测的数字范围......如果他猜测的数字低于随机选择的数字,它会打印再次尝试并猜测更高,反之亦然。
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 以便他可以选择是否要重新启动“游戏”
解决方案
你可以有一个输入语句询问“你想继续玩还是退出?”
这将是您的整个代码:
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
希望这是你想要的!
推荐阅读
- elasticsearch - Elasticsearch multi 应该使用 multi minimum_should_match 查询
- azure - Azure 数据工厂在考虑 DST 的情况下转换时间戳
- ios - 获取应用关闭时所有推送通知的列表 iOS
- android - 在前台播放 ExoPlayer
- c++ - 未加载节点 gyp 库:/usr/local/lib/libmtp.9.dylib
- android - 虽然 enableProguardInReleaseBuilds 为 true ,但构建失败
- java - 收到此错误:“无法初始化类 org.apache.logging.log4j.LogManager”
- typescript - 如何检测 Angular8 服务中对象的变化?
- php - TYPO3:使用构造函数将服务类注入 AuthServiceClass
- ios - 如何使用 alamofire 以 json 的顺序快速发布请求?