Day 022 - Project 22 : Totally Random Million-to-One

If you have any questions, comments or issues with this project please post them here!

Guess the number between one and 1Million!! Closest guess gets a shout-out from Replit team @LessonHacker and @AarrrMe!

https://replit.com/@JackAdem/Day-022-Project-22-Totally-Random-Million-to-One?v=1

Day 22 of #Replit100DaysOfCode #100DaysOfCode.

2 Likes
print("What's my number?")

import random
guess=random.randint(1,10)
guess_no = 0

print("What is your guess?", guess)

while True:
  myNumber=5
  if guess<=myNumber:
      print("Too high, try again.")
      guess_no += 1
    
    
  elif guess <= myNumber:
      print("Too low, try again.")
      guess_no += 1
    
     
  else:
     if guess == myNumber:
      print("Yippee, you got it in" , guess_no)

I need help, I’m not ashamed to admit it… this just created an endless loop and I don’t know why…

So you used <= which means less than or equal to. So when your guess is lower than the number or you guessed correct, it says “Too high, try again.”

Instead the code should be more like:

import random

number = random.randint(1, 1_000_000)
guess = 0
guessNumber = 0

while guess != number:
  if guess > number:
    print("Too high, try again.")
  else:
    print("Too low, try again.")

  guessNumber += 1

print("Yippee, you got it in", guessNumber)
1 Like

this produces another loop of either “Too low, try again” or “Too high, try again” depending on what the number is.

  • I"m not getting it… did it work for you?

I somehow forgot to get the user’s input. :woman_facepalming:

import random

number = random.randint(1, 1_000_000)
guess = 0
count = 0

print("Guess the number!")

while guess != number:
  guess = int(input("\nEnter a number > "))
  
  if guess > number:
    print("Too high, try again.")
  elif guess < number:
    print("Too low, try again.")

  count += 1

print("Yippee, you got it in", count)

1 Like

oh man, I totally missed the point there, I thought the user had to generate a new random number each time and if somehow the number they generated matched my number they were correct. When really they had to match the random number that was generated at the start.

“Make the number generator completely random between 1 and 1,000,000. Now, the game will always have the user guess a random number each time (now the user can’t cheat…)”

I read that as guessing a random number each time they guessed, not each time the game was played.

Thanks so much for your help.

2 Likes