0

Sorry in advance if my code is poorly written. I'm trying to make a program that allows you to add numbers to a list, and then provide the user to add more numbers to that same list. Here is what I have:

inputgameoption="y"

while inputgameoption=="y":

    ###this is where the user inputs the first number
    creepscore=eval(input("Finished a game? Type your creep score first then press enter.     Next, enter the duration of your game and press enter "))

    ###this is where the user inputs the second number
    gameduration=eval(input("Game duration should be rounded to the nearest minute, [ex. 24:25 becomes 24] "))

    cs=[]

    times=[]

    cs.append(creepscore)

    times.append(gameduration)

    inputgameoption=input("Enter another game?")

It works fine the first time, but if you say you want to enter more numbers (The enter another game input) it replaces your first input with your second input, and the list will stay with only one numbe in it. Thanks, I'm a python newbie.

4
  • 1
    DO NOT USE EVAL! you need to separate your code into functions Commented Aug 13, 2014 at 22:42
  • what is game duration? Commented Aug 13, 2014 at 22:50
  • In the case of my program it is the length of a league of legends game, which can be anywhere from 20-70 minutes. Commented Aug 13, 2014 at 22:53
  • how is the input entered, in what format? You have to be careful when casting strings, your program will crash if a value cannot be converted Commented Aug 13, 2014 at 22:55

3 Answers 3

3

Use a while loop. Also just cast their input to int instead of using eval.

# initialize the arrays outside the loop
cs = []
times = []

# loop for input until the user doesn't enter 'y'
inputgameoption = 'y'
while(inputgameoption == 'y'):
    creepscore = int(input("Finished a game? Type your creep score first then press enter.     Next, enter the duration of your game and press enter "))
    gameduration = int(input("Game duration should be rounded to the nearest minute, [ex. 24:25 becomes 24] "))

    # add their current inputs
    cs.append(creepscore)
    times.append(gameduration)

    # prompt to continue
    inputgameoption=input("Enter another game?")

print 'creepscores : ', cs
print 'times : ', times
Sign up to request clarification or add additional context in comments.

Comments

2

Your lines cs=[] and times=[] are replacing whatever was in those lists with empty ones. Move those before the loop.

Comments

2

The formatting of your code is rather unclear, however you just need to define the lists before the while loop like so:

inputgameoption="y"

cs=[]
times=[]

while inputgameoption=="y":

    ###this is where the user inputs the first number
    creepscore=int(input("Finished a game? Type your creep score first then press enter.     Next, enter the duration of your game and press enter "))

    ###this is where the user inputs the second number
    gameduration=int(input("Game duration should be rounded to the nearest minute, [ex. 24:25 becomes 24] "))



    cs.append(creepscore)
    times.append(gameduration)

    inputgameoption=input("Enter another game?")

3 Comments

-100 for using eval, cannot believe this was upvoted
eval shouldn't be used, however I was correcting what he stated was the problem.
does not matter, not correcting the obviously novice OP's badly written code was a bad attempt at an answer, I rescinded my downvote but your first answer deserved it

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.