Python Forum
A quick question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: A quick question (/thread-12667.html)



A quick question - teczone - Sep-06-2018

Hello, I would like to know how to make a loop that allows you to repeat yourself only when the player has input an answer:
import random
print("Bienvenue dans le juste prix !")
randomnumber = random.randint(1, 1000)
print(randomnumber)

print("Le nombre à était choisi par la machine. Tante de le trouver !")
numberplayer = int(input("Entrez un nombre entre 1 - 1000"))
LOOP HERE-LOOP HERE-LOOP HERE-LOOP HERE-LOOP HERE
if numberplayer < randomnumber:
    print("Le nombre est plus grand.")
elif numberplayer == randomnumber:
    print("Vous êtes le gagnant !")
elif numberplayer > randomnumber:
    print("Le nombre est plus petit")
else:
    print("Recommencer")



RE: A quick question - snippsat - Sep-06-2018

Basic idea,can use !=(not equal).
Then loop run until guessed the secret number,so no need to break out of the loop.
import random

secret_number = random.randint(1, 100)
tries, guess = 0, 0
while guess != secret_number:
   guess = int(input("Take a guess: "))
   # Lower or Higer(if,elif) when secret_number is guess the loop end



RE: A quick question - teczone - Sep-06-2018

(Sep-06-2018, 02:09 PM)snippsat Wrote: Basic idea,can use !=(not equal).
Then loop run until guessed the secret number,so no need to break out of the loop.
import random

secret_number = random.randint(1, 100)
tries, guess = 0, 0
while guess != secret_number:
   guess = int(input("Take a guess: "))
   # Lower or Higer(if,elif) when secret_number is guess the loop end

Actually, you didn't understand very well:
I don't know if you know the game of the right price, but in any case if you put a lower number than the right number, it should say whether it's smaller or larger.


RE: A quick question - j.crater - Sep-06-2018

To me snippsat's answer seems right for what you want to achieve.
Can you elaborate on why his answer doesn't help, and what result you want instead?


RE: A quick question - snippsat - Sep-06-2018

(Sep-06-2018, 02:21 PM)teczone Wrote: but in any case if you put a lower number than the right number, it should say whether it's smaller or larger.
It was a hint,the meaning was that you should do the rest.
Then meaning was that it should say smaller or larger until number is guessed.
Can just finish it,so can if it was on the right track.
import random

secret_number = random.randint(1, 100)
tries, guess = 0, 0
while guess != secret_number:
   guess = int(input("Take a guess: "))
   if guess > secret_number:
       print("The number is smaller")
   elif guess < secret_number:
       print("The number is bigger.")
   tries += 1

print(f'You guessed it! The number was {secret_number} in {tries} tries')
Output:
λ python guess_number.py Take a guess: 50 The number is smaller Take a guess: 25 The number is smaller Take a guess: 15 The number is smaller Take a guess: 7 The number is bigger. Take a guess: 10 The number is bigger. Take a guess: 13 You guessed it! The number was 13 in 6 tries