xsshii
xsshii

Reputation: 1

While loop keeps printing constantly, doesn't allow for user input

I'm trying to complete this assignment asking user for a number and if it's not -1 then it should loop. if it's -1 then to calculate the average of the other numbers. I'm getting stuck with the actual loop - it endlessly keeps printing the message to user to enter a different number - as in the picture - and doesn't give user a chance to enter a different number. Please help, I've been through so many videos and blogs and can't figure out what's actually wrong.

#creating a list for later calculations:
wrong = []

#asking for input:
input("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\nIf not correct you'll have to guess again ^-^")
num =int(input("number:"))

#looping
while num != -abs(1):
  wrong.append(num)
  print("Nope, guess again:")
  if num == -abs(1):
    break
    av = sum(wrong) / len(wrong)
    print ("You got it! The average of your wrong answers is: ")
    print(av)
    print("The End")
print("Nope, guess again:")

Upvotes: 0

Views: 1186

Answers (4)

Kkameleon
Kkameleon

Reputation: 313

You need to include the input function in the loop if you want it to work. I corrected the rest of your code as well, you don't need the if condition. More generally you should avoid to use break, it often means you are doing something wrong with your loop condition. Here it is redondant and the code after break is never executed.

wrong = []

input("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\nIf not correct you'll have to guess again ^-^")
num = int(input("Number: "))

while num != -1 :
  wrong.append(num)
  num = int(input("Nope, guess again: "))

av = sum(wrong) / len(wrong)
print(f"You got it! The average of your wrong answers is: {av}\nThe End")

Upvotes: 1

Cham
Cham

Reputation: 116

You should ask for input inside your loop, but you just print "Nope, guess again:".

wrong = []

print("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\n"
      "If not correct you'll have to guess again ^-^")

num = int(input("number: "))

# looping
while num != -1:
    wrong.append(num)
    num = int(input("Nope, guess again: "))

av = sum(wrong) / len(wrong)
print(f"You got it! The average of your wrong answers is: {av}\nThe End")

Upvotes: 0

user9544610
user9544610

Reputation: 19

There are lots of issues in the code. If you want to get inputs in while looping, you should include getting input code inside the while loop like below,

while num != -1:
 ......
 num =int(input("number:"))
 ......

Also you don't have to include 'break' inside the while loop because, when num != 1, the loop will stop.

Upvotes: 0

hassan abbas
hassan abbas

Reputation: 72

You are just breaking the loop before printing the results, first print the results, then break the loop.

And a while loop isn't necessary for your program, use if condition wrapped in a function instead:

#creating a list for later calculations:
wrong = []

#asking for input:
input("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\nIf not correct you'll have to guess again ^-^")


#looping
def go():
    num =int(input("number:"))
    if num != -abs(1):
      wrong.append(num)
      print("Nope, guess again:")
      if num == -abs(1):
        av = sum(wrong) / len(wrong)
        print ("You got it! The average of your wrong answers is: ")
        print(av)
        print("The End")
        break
    print("Nope, guess again:")
    go()

Upvotes: 0

Related Questions