簡體   English   中英

如何使 if 語句檢查每個 for 循環語句的有效輸入

[英]How to make if statement check each for loop statement for valid input

import random
max_value = input("I'm going to pick a number. You have to try and guess the same number that I pick. Guess right and win a prize. What is the highest number I can pick? ")
computer_choice = random.randint(1, int(max_value))

for (computer_choice) in range(1,6):
    user_choice = input("What number between 1 and " + max_value + " do you choose? ")
    if user_choice == computer_choice:
        print("Thank you for playing. ")

在失敗之前需要給用戶 5 次機會來給 computer_choice。 需要 for 循環。

您可以添加一個計數器來跟蹤用戶到目前為止所做的嘗試次數。

還需要將用戶輸入從string 類型轉換為int 類型。

import random
max_value = int(input("I'm going to pick a number. You have to try and guess the same number that I pick. Guess right and win a prize. What is the highest number I can pick? "))
computer_choice = random.randint(1, int(max_value))
count = 0
for (computer_choice) in range(1,6):
    user_choice = int(input("What number between 1 and " + max_value + " do you choose? "))
    count += 1
    if user_choice == computer_choice:
        print("Thank you for playing. ")
    if count==5:
         print("you have reached maximum attempt limit")
         exit(0)

使用單獨的變量運行 for 循環,然后運行 computer_choice。 還要在輸入語句中添加 eval,因為它提供字符串以將 user_choice 轉換為 integer,並在 if user_choice == computer_choice 中添加 break 語句以結束程序,rest 應該可以正常工作。

import random
max_value = input("I'm going to pick a number. You have to try and guess the same number that I pick. Guess right and win a prize. What is the highest number I can pick? ")
computer_choice = random.randint(1, int(max_value))

for (i) in range(1,6):
    #input takes string convert choice to int using eval
    user_choice = eval(input("What number between 1 and " + max_value + " do you choose? "))
    if user_choice == computer_choice:
        print("Thank you for playing. ")
        break
    if(i==5):
        print("Sorry, maximum number of tries reached.")
        

方法之一:

import random
max_value = input("I'm going to pick a number. You have to try and guess the same number that I pick. Guess right and win a prize. What is the highest number I can pick? ")
computer_choice = random.randint(1, int(max_value))

for i in range(1,6):
    user_choice = input("What number between 1 and " + max_value + " do you choose? ")
    if int(user_choice) == int(computer_choice):
        print("Right choice. Thank you for playing. ")
        break
    elif(i == 5):
        print ("Sorry, maximum attempts reached...")
        break
    else:
        print ("Oops, wrong choice, pls choose again...")

Output:

I'm going to pick a number. You have to try and guess the same number that I pick. Guess right and win a prize. What is the highest number I can pick? 5
What number between 1 and 5 do you choose? 1
Oops, wrong choice, pls choose again...
What number between 1 and 5 do you choose? 3
Oops, wrong choice, pls choose again...
What number between 1 and 5 do you choose? 4
Oops, wrong choice, pls choose again...
What number between 1 and 5 do you choose? 5
Oops, wrong choice, pls choose again...
What number between 1 and 5 do you choose? 6
Sorry, maximum attempts reached...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM