2017-01-15 22 views
-1

我想在python中進行「猜數字」遊戲,其中我選擇最小和最大數字,如果我選擇的數字更低或更高,我希望它重複該問題,我怎樣才能做到這一點?如果輸出爲假,則重複輸入

這裏是我的代碼:

import random 
import time 

print("Welcome to the guessing game!") 

time.sleep(1) 

print("Choose your minumum number") 

minnum=input("Min: ") 

print(" ") 

print("Choose your maximum number") 

maxnum=input("Max: ") 

print(" ") 

print("Enter your number") 

num = input("Number: ") 

print(" ") 

q = random.randint(int(minnum), int(maxnum)) 

def game(): 
    if int(num) < q: 
     print("The number is higher") 

    if int(num) > q: 
     print("The number is lower") 

    if int(num) == q: 
     print("Congratulations! you won!") 
     break 
game() 
print(" ") 
print(" ") 
input("Press enter to exit") 
+0

你不能有外部的'break'聲明的循環。 – Tagc

回答

0

這種變化執行額外的驗證,如果你想的是:

from random import randint 


def request_user_input(query): 
    while True: 
     try: 
      return int(input(query)) 
     except ValueError: 
      continue 

def run_game(target): 
    while True: 
     guess = request_user_input('Enter your number: ') 
     if guess < target: 
      print('The number is higher') 
     elif guess > target: 
      print('The number is lower') 
     else: 
      print('Congratulations! You won!') 
      break 

if __name__ == '__main__': 
    min = request_user_input('Min: ') 
    max = request_user_input('Max: ') 
    if max < min: 
     raise ValueError('The maximum value cannot be smaller than the minimum') 

    run_game(target=randint(min, max + 1)) 

示例執行

Min: a 
Min: 10 
Max: 20 
Enter your number: 5 
The number is higher 
Enter your number: 25 
The number is lower 
Enter your number: 15 
The number is higher 
Enter your number: 18 
The number is higher 
Enter your number: 20 
The number is lower 
Enter your number: 19 
Congratulations! You won! 
1

移動遊戲裏面輸入(),並使其循環,像這樣:

def game(): 
    while True: 
     print("Enter your number") 

     num = input("Number: ") 

     if int(num) < q: 
      print("The number is higher") 

     if int(num) > q: 
      print("The number is lower") 

     if int(num) == q: 
      print("Congratulations! you won!") 
      break 
0

你需要一個while循環,以保持持續,如果用戶有輸入了錯誤的猜測,如果正確,環路將退出break

import random 
import time 

print("Welcome to the guessing game!") 
time.sleep(1) 
print("Choose your minumum number") 
minnum=input("Min: ") 
print(" ") 
print("Choose your maximum number") 
maxnum=input("Max: ") 
print(" ") 
q = random.randint(int(minnum), int(maxnum)) 

def game(): 
    while True: #while loop for looping continuously until correct input 
     print("Enter your number") 
     num = input("Number: ") 
     print(" ") 

     if int(num) < q: 
      print("The number is higher") 

     if int(num) > q: 
      print("The number is lower") 

     if int(num) == q: #if answer correct stop looping 
      print("Congratulations! you won!") 
      break 

game() 
print(" ") 
print(" ") 
input("Press enter to exit") 

輸出:

Welcome to the guessing game! 
Choose your minumum number 
Min: 1 

Choose your maximum number 
Max: 5 

Enter your number 
Number: 3 

The number is lower 
Enter your number 
Number: 4 

The number is lower 
Enter your number 
Number: 5 

The number is lower 
Enter your number 
Number: 2 

Congratulations! you won!