1

I am new to this website so apologies if I have formatted my code wrong. I am having some trouble with my program; I want to have Player One able to re-enter their number if their first entry was greater than ten. My program seems to end after the first if statement.

Is there any particular statement I could use to get this job done?

Thanks.

#Program that gives Player2 five guesses to guess Player1's number.

import time

Number_To_Guess = int(input('Player One Enter Your Chosen Number: '))
if Number_To_Guess > 10:
    print('Your Number Must Be Less Than 10')
elif Number_To_Guess < 10:
    Player_Two_Guess = int(input('Player Two Guess The Number: '))

time.sleep(3)
0

2 Answers 2

0

Program that gives Player2 five guesses to guess Player1's number.

This will work until user got right guess:

import time

Number_To_Guess = int(input('Player One Enter Your Chosen Number: '))
if Number_To_Guess > 10:
    print('Your Number Must Be Less Than 10')
elif Number_To_Guess < 10:
    Player_Two_Guess = 0
    TotalGuesses = 0
    while Player_Two_Guess != Number_To_Guess and TotalGuesses < 5:
        TotalGuesses += 1
        Player_Two_Guess = int(input('Player Two Guess The Number: '))
        time.sleep(3)

But i would reduce the sleep time :D
Added a counter to reduce the number of guesses to 5.

7
  • And when did Python have && for logical and and ++ for post increment operators? Mar 29, 2015 at 17:49
  • gee... that is what happens when jumping from language to language :D Mar 29, 2015 at 18:12
  • hmm. not fair for voting down, as this is the only sample, that actually runs as desired allowing for multiple guesses :-/ Mar 29, 2015 at 18:27
  • @hexereisoftware you know it wasn't me who voted down right, I am very appreciative of your response? :)
    – hairen-v
    Mar 29, 2015 at 21:06
  • @Virolite i know :) i think i know who did, but it is ok - i had a typing error in there for 20 mins which would have caused error Mar 29, 2015 at 21:08
0

An infinite loop will do the work:

while True:
    Number_To_Guess = int(input('Player One Enter Your Chosen Number: '))
    if Number_To_Guess > 10:
        print('Your Number Must Be Less Than 10')
    else:
        break
Player_Two_Guess = int(input('Player Two Guess The Number: '))
8
  • this will actually do an infinite loop on entering the number to guess but not for the guesses Mar 29, 2015 at 13:54
  • Thnaks for your help.
    – hairen-v
    Mar 29, 2015 at 14:08
  • @hexereisoftware See the break statement?
    – wingerse
    Mar 29, 2015 at 17:37
  • yes, but the line where player should guess number is not repeated, and to complete the "game" it should be repeated until Player_Two_Guess is equal to Number_To_Guess Mar 29, 2015 at 17:41
  • you could just add while Player_Two_Guess != Number_To_Guess: before the last line, indent it and there you go Mar 29, 2015 at 17:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.