Reputation: 5
So im having a little trouble with a project im working on. I'm not an expert with Python nor am I an idiot when it comes to coding. This problem may have a very simple answer but I cant seem to get it right. My entire code asks a user to answer questions using a random choice from a list.
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")
in this bit of code, it asks the user how many questions they want to ask (as an integer). My code works well when a number within the parameters are given, but as soon as a number such as 19 is given, it continues when it should not. Also if a string is given, it works well and asks again for an integer, but if an integer is given after being asked, it crashes. The error read:
for i in range(int(num)):`ValueError: invalid literal for int() with base 10: 'test'`
All help would appreciated so much. thank you all
Upvotes: 0
Views: 134
Reputation: 1215
EDIT: @see xbello answer to have a working answer. My "poisoned" code isn't working as expected ;)
First of all I must say that your code isn't really nice to read... But here are some fixes that should do the trick
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
You can see that I added a return value to your function (that returned None by default) and that I take this return value to push it into "numbers" afterwards. That "numbers" value can then be pushed into a range() function to make a nice for loop.
Upvotes: 0
Reputation: 7443
And you're hidding too much code within a generic try..except (try to Ctrl+C while input is required... nope!). I would write that function in that way:
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()
Upvotes: 1
Reputation:
getNumbers()
doesn't return any value. Thus, it implicitly returns None
, which you assign to numbers
, here:
numbers = getNumbers(numbers)
Make sure that wherever you exit the getNumbers()
function, you return numbers
(probably at the end:
def getNumbers(numbers):
....
return numbers
Upvotes: 0