Reputation: 21
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.
Upvotes: 0
Views: 92
Reputation: 1188
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?")
Upvotes: 2
Reputation: 118001
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
Upvotes: 3
Reputation: 1007
Your lines cs=[]
and times=[]
are replacing whatever was in those lists with empty ones. Move those before the loop.
Upvotes: 2