-2

My python program asks the user to input any random number, stores them in the appropriate variables, then displays them in the final statement. I have no idea why the actual variables do not show the counted inputs. I am stuck and confused.

countOne = 0
countTwo = 0
countThree = 0
countFour = 0
countFive = 0
countSix = 0    


print("Welcome")
print("This program is determines how often a certain number appears after the dice have been tossed")
print("You will toss your dice then input the number rolled. After you have entered in the information, the program will tell you how many times the number appears")
userInput=int(input("Please enter the number shown(1-6) or enter 0 to quit: "))



for i in range(1, 6):
    if userInput == 1:
        countOne = countOne + 1
    elif userInput == 2:
        countTwo = countTwo + 1
    elif userInput == 3:
        countThree = countThree + 1
    elif userInput == 4:
        countFour = countFour + 1
    elif userInput == 5:
        countFive = countFive + 1
    elif userInput == 6:
        countSix = countSix + 1

while userInput != 0:
    if userInput >= 0 and userInput <= 6:
        userInput=int(input("Please enter the number shown(1-6) or enter 0 to quit: "))
    else:
        userInput=int(input("ERROR, NUMBER OUTSIDE RANGE!! Please enter the number shown(1-6) or enter 0 to quit: "))
print("Number 1 appeared: ",countOne," time.")
print("Number 2 appeared: ",countTwo," time.")
print("Number 3 appeared: ",countThree," time.")
print("Number 4 appeared: ",countFour," time.")
print("Number 5 appeared: ",countFive," time.")
print("Number 6 appeared: ",countSix," time.")
print("Thank you! Good Bye!")

It runs with no syntax error but will reply with the incorrect number.

user3758438
  • 3
  • 1
  • 4
  • It is supposed to count any random number that the user inputs. For example, my last set of inputs had 4 3's but would only show 0 3's. I don't think I can take out too much from this code that would still allow it to work without syntax errors. So, I don't think that minimizing it would reduce it greatly – user3758438 Jul 02 '14 at 19:27

1 Answers1

2

Your loops make no sense:

userInput=int(input("Please enter the number shown(1-6) or enter 0 to quit: "))
for i in range(1, 6):
    ...
while userInput != 0:
    ...

You take input once, then count the same input five times, then take more input (without counting it). Also, having six separate variables is unnecessary; just use a list of six integers.

Try separating your input validation into a separate function (see e.g. this community wiki) then do something like:

counts = [0 for _ in range(6)]
while True:
    ui = get_input() # input function validates 0 to 6
    if ui == 0:
        break
    else:
        counts[ui-1] += 1 # note -1 - list is zero-based

Note that using a list removes a huge amount of duplication:

print("Number 1 appeared: ",countOne," time.")
print("Number 2 appeared: ",countTwo," time.")
print("Number 3 appeared: ",countThree," time.")
print("Number 4 appeared: ",countFour," time.")
print("Number 5 appeared: ",countFive," time.")
print("Number 6 appeared: ",countSix," time.")

becomes:

for num, count in enumerate(counts, 1):
    print("Number {0} appeared: {1} time.".format(num, count))
Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437