所以即时通讯正在处理一个项目。我不是Python的专家,也不是编码方面的白痴。这个问题可能有一个非常简单的答案,但我似乎无法做到正确。我的整个代码要求用户使用列表中的随机选择来回答问题。
import turtle
import random
turtle.speed("fastest")
pi = 3
minNumber = 5
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/userAnswer)
turtle.penup()
turtle.setpos(-700,-200)
turtle.fillcolor("green")
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")
num = input("how many questions would you like to answer? (Pick between 5 and 10)")
def getNumbers(numbers):
try:
badInput = False
while not (badInput):
num = input("how many questions would you like to answer? (Pick between 5 and 10)")
numbers = int(num)
badInput = (numbers >= 4) or (numbers >= maxNumber)
if badInput == False:
print ("Please input an integer between 5 and 10 please")
badInput = False
except:
print("Please input an integer between 5 and 10")
numbers= 0;
numbers = getNumbers(numbers)
numbers= 0;
numbers = getNumbers(numbers)
for i in range(int(num)):
red = random.random()
green = random.random()
blue = random.random()
num1 = random.choice(a)
turtle.color(red,green,blue)
correct = num1 * 3
print("What is the cirumference of the circle if", num1,"is the diameter and Pi is 3?")
userAnswer = int(input())
if userAnswer == correct:
print("That's Correct! Well Done")
score = score + 1
for k in range(correct):
turtle.color(red,green,blue)
drawSquare()
turtle.penup()
turtle.forward(150)
else:
print("sorry thats is incorrect")
在这段代码中,它询问用户他们想要问多少问题(作为整数)。当给出参数中的数字时,我的代码运行良好,但是一旦给出诸如19的数字,它就会在不应该的情况下继续。此外,如果给出了一个字符串,它会运行良好并再次询问一个整数,但如果在被询问后给出一个整数,它就会崩溃。错误读取:
for i in range(int(num)):`ValueError: invalid literal for int() with base 10: 'test'`
所有帮助都会非常感激。谢谢大家
答案 0 :(得分:1)
你在一般的try..except中隐藏了太多代码(尝试按Ctrl + C,同时输入是必需的......不!)。我会以这种方式写这个函数:
def getNumbers():
num = input("how many questions would you like to answer? (Pick between 5 and 10)")
try:
number = int(num)
except:
print("Not a number!")
return getNumbers()
goodInput = minNumber < number < maxNumber
if not goodInput:
print ("Please input an integer between 5 and 10 please")
return getNumbers()
else:
return number
number = getNumbers()
答案 1 :(得分:0)
getNumbers()
不会返回任何值。因此,它隐式返回None
,您在此处指定numbers
:
numbers = getNumbers(numbers)
确保无论何时退出getNumbers()
功能,都会返回numbers
(可能在最后:
def getNumbers(numbers):
....
return numbers
答案 2 :(得分:0)
首先,我必须说你的代码并不是很好阅读......但是这里有一些应该可以解决的问题
maxNumber = 10
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")
num = input("how many questions would you like to answer? (Pick between 5 and 10)")
def getNumbers(numbers):
try:
goodInput = False
while not (goodInput):
num = input("how many questions would you like to answer? (Pick between 5 and 10)")
numbers = int(num)
# Here was a bad condition having a look at your comments
goodInput = (numbers > 4) and (numbers <= maxNumber)
if goodInput == False:
print ("Please input an integer between 5 and 10 please")
# goodInput is already False, no need to set it again
# Here is the missing return
return numbers
except:
print("Please input an integer between 5 and 10")
numbers= 0;
numbers = getNumbers(numbers)
numbers= 0;
numbers = getNumbers(numbers)
for i in range(numbers):
#Do stuff
你可以看到我为你的函数添加了一个返回值(默认情况下返回None),然后我将这个返回值推送到“数字”中。然后可以将“数字”值推送到range()函数中以形成一个很好的for循环。