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
for i in range(int(num))which is not shown in the code above. But the error is clear: you're trying to convert the string 'test' to an integer, that can't work.(numbers <= 4) or (numbers >= maxNumber)or(numbers >= 4) and (numbers < maxNumber)depending on whether you want badInput or goodInput. Also, you don't mention what maxNumber is... and whether the maxNumber itself should be considered in the set.