2

I'm making a guessing game or computer science in school where the number to guess is seven. I have tried using while loops and if elif else statements but it doesn't seem to want to make a conditional loop My code is as follows:

guess=int(input("Guess a number!"))
var=1
while var==1:
    if guess !=7:
        print("Try again")
    else:
        print("Well done")

Any help would be appreciated thanks. I need it in about a week and a half's time.

1
  • 1
    Consider: where in your code do you ask the user for input? Does that allow the user to input data more than once? Commented May 4, 2018 at 20:47

2 Answers 2

1

If you're trying to allow your player to continuously guess the input needs to be at the top of the while loop, before the conditional-branch

while(True):
    guess = input("Make a guess: ")
    if(guess == 7):
        print(guess,"was correct!")
        break
    else:
        print("Nope. Guess again.")

Of course, you could make it more interesting in a variety of ways.

Sign up to request clarification or add additional context in comments.

Comments

0
guess=int(input("Guess a number!"))
var=1
while var==1:
    if guess !=7:
        print("Try again")
        guess=int(input("Guess a number!"))
    else:
        print("Well done")
        var=0 #set var to 0, to exit the loop

Try this. You need to exit the loop, and to do that, var needs to be set to 0.

3 Comments

I tried it, but with that, if I guess wrong then it just keeps printing "Try again" over and over.
Sorry, forgot to put the input again :). Try it now
Yes! Thank you so much this works! Appreciate your help and everyone else who helped!:D!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.