0

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

7
  • For the string error, you should post your full code, we cannot imagine it... For the other one, same answer, we don't have the value of "maxNumber" so if that value is 1000 badInput is not false when you type 19... Commented Sep 5, 2014 at 9:50
  • Essential code parts are missing: you show an error around a line with 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. Commented Sep 5, 2014 at 9:53
  • sure, ill upload the entire code. Check the post again, and it should be updated. Commented Sep 5, 2014 at 9:53
  • By the way you should rename your variable "badInput" to "goodInput". If it's defaulting to "False" then it's by default a goodInput :) for reading purposes you should have either "while not goodInput" or "while badInput". Commented Sep 5, 2014 at 9:56
  • You've still got your comparisons wrong, it should either be (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. Commented Sep 5, 2014 at 10:00

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

@brandon Normal usage is to accept the answer if it solved your problem. That would (also) be thanking xbello.
hehe sorry im new to this.
0

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

Comments

0

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.

2 Comments

This fails because it's "poisoned" with the original code: even if your first input is correct (line 4), it asks again for another number. If you enter some text and then a goodInput, you get None because you exited the while through exception at num = int(num), that assigns None to the outer numbers and then run again the getNumbers. You must change numbers = getNumbers(numbers) within except for return getNumbers(numbers).
You're right, the basic code structure is so "strange" I didn't realize that :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.