Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop keeps looping
#1
Hi All,

Really new to Python, and doing some coding to get use/understand it. I am working on a program, that creates a random number and the user needs to guess that random number. However the code I have done so far doesn't seem to work. Fo some reason the code keeps looping, even though I enter the correct number:

import random
attempts = 0
guess = 0
number = random.sample(range( 1,100),1)
print("Welcome to the random number guessing game!")
print(number)
while guess != number:
  guess = int(input("Please enter a number to guess: "))
  attempts +=1
else:
  print("Well done, you guesed the number")
Any idea why would be very much appreciated.

Thanks
Reply


Messages In This Thread
While loop keeps looping - by mcoliver88 - Jul-29-2020, 12:33 PM
RE: While loop keeps looping - by buran - Jul-29-2020, 12:47 PM
RE: While loop keeps looping - by mcoliver88 - Jul-29-2020, 12:47 PM
RE: While loop keeps looping - by buran - Jul-29-2020, 12:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "while" loop is looping back too early mangurian 1 1,395 Jan-28-2022, 09:15 AM
Last Post: ibreeden
  Nested for loop not looping puttingwordstogether 0 1,807 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  while loop will not stop looping TheTechRobo 5 3,982 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  'Looping' does not work out within a 'for Loop' Placebo 4 3,590 Sep-15-2018, 08:19 PM
Last Post: Placebo
  For looping over a list, editing the list from inside the loop? Krookroo 3 4,120 Sep-04-2017, 05:08 PM
Last Post: Krookroo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020