Python 询问用户是否想再玩一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26961427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:13:50  来源:igfitidea点击:

Asking the user if they want to play again

pythonloopsrandom

提问by DesiBoy

Basically it's a guessing game and I have literally all the code except for the last part where it asks if the user wants to play again. how do I code that, I use a while loop correct?

基本上这是一个猜谜游戏,除了最后一部分询问用户是否想再次玩外,我几乎拥有所有代码。我该如何编码,我使用了正确的 while 循环?

heres my code:

继承人我的代码:

import random
number=random.randint(1,1000)
count=1
guess= eval(input("Enter your guess between 1 and 1000 "))

while guess !=number:
count+=1

 if guess > number + 10:
  print("Too high!")
 elif guess < number - 10:
  print("Too low!")
 elif guess > number:
  print("Getting warm but still high!")
 elif guess < number:
  print("Getting warm but still Low!")

 guess = eval(input("Try again "))
print("You rock! You guessed the number in" , count , "tries!")

while guess == number:
 count=1
 again=str(input("Do you want to play again, type yes or no "))
if again == yes:

guess= eval(input("Enter your guess between 1 and 1000 "))

猜测= eval(input("输入你的猜测1到1000"))

if again == no:
break

回答by ????

Don't use eval(as @iCodex said) - it's risky, use int(x). A way to do this is to use functions:

不要使用eval(如@iCodex 所说)- 有风险,请使用int(x). 一种方法是使用函数:

import random
import sys

def guessNumber():
    number=random.randint(1,1000)
    count=1
    guess= int(input("Enter your guess between 1 and 1000: "))

    while guess !=number:
        count+=1
        if guess > (number + 10):
            print("Too high!")
        elif guess < (number - 10):
            print("Too low!")
        elif guess > number:
            print("Getting warm but still high!")
        elif guess < number:
            print("Getting warm but still Low!")

        guess = int(input("Try again "))

    if guess == number:
        print("You rock! You guessed the number in ", count, " tries!")
        return

guessNumber()

again = str(input("Do you want to play again (type yes or no): "))
if again == "yes":
    guessNumber()
else:
    sys.exit(0)

Using functions mean that you can reuse the same piece of code as many times as you want.

使用函数意味着您可以根据需要多次重复使用同一段代码。

Here, you put the code for the guessing part in a function called guessNumber(), call the function, and at the end, ask the user to go again, if they want to, they go to the function again.

在这里,您将猜测部分的代码放在一个名为 的函数中guessNumber(),调用该函数,最后,让用户再去一次,如果他们愿意,他们再去一次该函数。

回答by Joran Beasley

separate your logic into functions

将您的逻辑分离成函数

def get_integer_input(prompt="Guess A Number:"):
    while True:
       try: return int(input(prompt))
       except ValueError:
          print("Invalid Input... Try again")

for example to get your integer input and for your main game

例如获取整数输入和主游戏

import itertools
def GuessUntilCorrect(correct_value):
   for i in itertools.count(1):
       guess = get_integer_input()
       if guess == correct_value: return i
       getting_close = abs(guess-correct_value)<10
       if guess < correct_value:
          print ("Too Low" if not getting_close else "A Little Too Low... but getting close")
       else:
          print ("Too High" if not getting_close else "A little too high... but getting close")

then you can play like

然后你可以玩

tries = GuessUntilCorrect(27) 
print("It Took %d Tries For the right answer!"%tries)

you can put it in a loop to run forever

你可以把它放在一个循环中永远运行

while True:
     tries = GuessUntilCorrect(27) #probably want to use a random number here
     print("It Took %d Tries For the right answer!"%tries)
     play_again = input("Play Again?").lower()
     if play_again[0] != "y":
        break

回答by Alu

One big while loop around the whole program

整个程序的一个大while循环

import random

play = True

while play:
  number=random.randint(1,1000)
  count=1
  guess= eval(input("Enter your guess between 1 and 1000 "))

    while guess !=number:
      count+=1

      if guess > number + 10:
        print("Too high!")
      elif guess < number - 10:
        print("Too low!")
      elif guess > number:
        print("Getting warm but still high!")
      elif guess < number:
        print("Getting warm but still Low!")

      guess = eval(input("Try again "))
    print("You rock! You guessed the number in" , count , "tries!")

    count=1
    again=str(input("Do you want to play again, type yes or no "))
    if again == "no":
      play = False

回答by user9123

I want to modify this program so that it can ask the user whether or not they want to input another number and if they answer 'no' the program terminates and vice versa. This is my code:

我想修改这个程序,以便它可以询问用户是否要输入另一个数字,如果他们回答“否”,则程序终止,反之亦然。这是我的代码:

step=int(input('enter skip factor: '))
num = int(input('Enter a number: '))

while True:
  for i in range(0,num,step):    

    if (i % 2) == 0: 
       print( i, ' is Even')
    else:
       print(i, ' is Odd')
again = str(input('do you want to use another number? type yes or no')
        if again = 'no' :     
            break