代码开始时,我让用户输入他想猜的数字范围……如果他猜的数字低于随机选择的数字,我会打印出来,然后再试一次,猜得更高,反之亦然
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,这样他就可以选择是否想要重新启动“游戏”
这是一个更简洁的代码版本。我还认为使用整数可能更有意义,并且只转换一次
你可以有一个输入语句,询问“你想继续玩还是退出?”
这将是您的全部代码:
希望这就是你想要的
相关问题 更多 >
编程相关推荐