en3s
en3s

Reputation: 1

Is There A Way To Keep My Code Running In Python

I was trying to make "Guess The Number" project. I did all of "You Need To Guess Higher/Lower" things But when i run the code it tells me to guess higher/lower and finishes. it does not give me the chance to continue or guess again i need the code to keep running how can i do that enter code here (thanks for everyone who replied & answered )

import random

random.randint(0, 100)

random = random.randint(0, 100)

guess = float(input("Guess The Number"))

if guess > random + 50:
    print("Too High")
elif guess > random + 25:
    print("A little High")
elif guess > random + 10:
    print("So Close")
elif guess + 50 < random:
    print("Too Low")
elif guess + 25 < random:
    print("A little low")
elif guess + 10 < random:
    print("So Close")
elif guess == random:
    print("Great Job")
else:
    print("Guess Again!")

Upvotes: 0

Views: 615

Answers (1)

Luke-zhang-04
Luke-zhang-04

Reputation: 793

I normally wouldn't answer this kind of question, but I try to be more lenient. Please search up a tutorial on looping. Control flow is a programming fundamental. Nevertheless, here's a solution. Please try to understand the code rather than just copy-pasting it.

import random

random = random.randint(0, 100)

guess = float(input("Guess The Number"))

while guess != random:
    if guess > random + 50:
        print("Too High")
    elif guess > random + 25:
        print("A little High")
    elif guess > random + 10:
        print("So Close")
    elif guess + 50 < random:
        print("Too Low")
    elif guess + 25 < random:
        print("A little low")
    elif guess + 10 < random:
        print("So Close")
    else:
        print("Guess Again!")
    guess = float(input("Guess The Number"))

print("Great Job")

Explanation:

while guess != random

is quite self-explanatory. Basically, the program underneath runs until guess != random. As soon as guess is equal to random the loop stops.

Upvotes: 3

Related Questions