我正在製作一個程序,使用列表爲用戶提供一個從1到10的隨機整數作爲直徑,並且他們必須使用Pi作爲3計算周長。到目前爲止,所有工作都正常除了有關糟糕投入的事情。當用戶回答問題時,無論答案如何,它都表示這是不正確的。在簡單的Python程序中處理用戶輸入
有機會得到一些幫助嗎?這裏是我的代碼:
import turtle
import random
turtle.speed("fastest")
pi = 3
minNumber = 4
maxNumber = 10
score = 0
listNmbers = []
a = [1,3,5,7,9]
red = random.random()
green = random.random()
blue = random.random()
num1 = random.choice(a)
def drawSquare():
for i in range(4):
turtle.begin_fill()
turtle.pendown()
turtle.forward(50)
turtle.left(90)
turtle.end_fill()
turtle.right(360/correct)
turtle.penup()
turtle.setpos(-700,-200)
turtle.fillcolor("green")
def askCir(cirAnswer):
try:
user = input("What is the circumference of a circle if the diameter is " + str(num1) + " and Pi is 3?")
cirAnswer = int(user)
except:
print("Please input a number only!")
cirAnswer = 0;
cirAanswer = askCir(cirAnswer)
print("Welcome! What is your name??")
name = str(input())
print("Hello", name,"you need to calculate the circumference of a circle when given a diameter. To calculate the circumference, use the equasion; Pi x Diameter (Pi = 3")
def getNumbers():
num = input("how many questions would you like to answer? (Pick between 5 and 10)")
try:
numbers = int(num)
except:
print("That is not a number!")
return getNumbers()
goodInput = minNumber < numbers < maxNumber
if not goodInput:
print ("That is not between 5 and 10. Please input an integer between 5 and 10.")
return getNumbers()
else:
return numbers
numbers = getNumbers()
for i in range(numbers):
red = random.random()
green = random.random()
blue = random.random()
turtle.color(red,green,blue)
num1 = random.choice(a)
correct = num1 * 3
cirAnswer = 0;
cirAnswer = askCir(cirAnswer)
print(str(correct))
if cirAnswer == correct:
print("That's Correct! Well Done")
score = score + 1
else:
print("Sorry that is incorrect")
for k in range(correct):
turtle.color(red,green,blue)
drawSquare()
turtle.penup()
turtle.forward(150)
我一直在輸入問題,所以試試raw_input我認爲它會有所幫助。我沒有深入你的代碼... – urosjarc 2014-09-06 09:44:41