python3.4彩票选取器直接通过if语句到达els

2024-10-01 11:40:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码运行良好,直到我尝试添加“response”,用户必须输入#“p”才能生成一个随机数6次。我无法从第一个if语句中识别变量“response”和“yournum”。你知道吗

就像我说的,只需使用“yournum”变量就可以了-我只想

用户点击“p”一次生成一行。你知道吗

import random
count = 0
response = 0
yournum = 0
print("Hello! Welcome to the Lottery Picker \n ")
print("Hit the letter P to pick each line or any other letter to quit ")
response = input()
while count < 6:
#response = 0
     if  yournum == 0 and response == "P":
         number = random.randint(1, 49)
         print("Your first number is: " + str(number))         
         count = count + 1
         yournum = yournum + 1      
     elif yournum == 1 and response == "p":
         number = random.randint(1, 49)
         print("Your second number is: " + str(number))         
         count = count + 1
         yournum = yournum + 1 
         print("Hit the letter P to another number ")
     #response = 0  
         response = input()        
     elif yournum == 2 and response == "p":
         number = random.randint(1, 49)
         print("Your third number is: " + str(number))         
         count = count + 1   
         yournum = yournum + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input() 
     elif yournum == 3 and response == "p":
         number = random.randint(1, 49)
         print("Your fourth number is: " + str(number))         
         count = count + 1  
         yournum = yournum + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input() 
     elif yournum == 4 and response == "p":
         number = random.randint(1, 49)
         print("Your fifth number is: " + str(number))         
         count = count + 1
         yournum = yournum + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input() 
     elif yournum == 5 and response == "p":
         number = random.randint(1, 49)
         print("And your powerball number is: " + str(number))      
         count = count + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input()          
     else:
         count = count + 1
         print("Something is wrong with the lottery picker - goodbye ")

Tags: andthetonumberinputisresponsecount
1条回答
网友
1楼 · 发布于 2024-10-01 11:40:28

在第一个if案例中,您缺少用户输入“p”和response = input()行的请求。另外,后面的if语句将根据小写“p”而不是像第一个一样的大写“p”检查响应。假设大写字母“P”是正确的,则更新的代码为:

import random
count = 0
response = 0
yournum = 0
print("Hello! Welcome to the Lottery Picker \n ")
print("Hit the letter P to pick each line or any other letter to quit ")
response = input()
while count < 6:
#response = 0
     if  yournum == 0 and response == "P":
         number = random.randint(1, 49)
         print("Your first number is: " + str(number))         
         count = count + 1
         yournum = yournum + 1      
         print("Hit the letter P to another number ")
     #response = 0  
         response = input()        
     elif yournum == 1 and response == "P":
         number = random.randint(1, 49)
         print("Your second number is: " + str(number))         
         count = count + 1
         yournum = yournum + 1 
         print("Hit the letter P to another number ")
     #response = 0  
         response = input()        
     elif yournum == 2 and response == "P":
         number = random.randint(1, 49)
         print("Your third number is: " + str(number))         
         count = count + 1   
         yournum = yournum + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input() 
     elif yournum == 3 and response == "P":
         number = random.randint(1, 49)
         print("Your fourth number is: " + str(number))         
         count = count + 1  
         yournum = yournum + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input() 
     elif yournum == 4 and response == "P":
         number = random.randint(1, 49)
         print("Your fifth number is: " + str(number))         
         count = count + 1
         yournum = yournum + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input() 
     elif yournum == 5 and response == "P":
         number = random.randint(1, 49)
         print("And your powerball number is: " + str(number))      
         count = count + 1
         print("Hit the letter P to another number ")
     #response = 0  
         response = input()          
     else:
         count = count + 1
         print("Something is wrong with the lottery picker - goodbye ")

相关问题 更多 >