Python Forum
Need help with the while function(Beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with the while function(Beginner)
#1
while guess_count<guess_most:
        answer=input('Guess a number ')
        guess_count+=1
        if str(answer)==str(winning_number):
            print('Correct!')
            break
I know this is a very simple problem but as I stated in the thread subject, I am a beginner. I was trying to make the classic 'guess the number' game and I want to add a function so that when i answer wrong in the first try, I want it to say 'try again' instead of 'guess a number'. I hope you can help, thanks.
Reply
#2
FYI indentation should be 4 spaces per level.
guess_count = 0
guess_most = 3

while guess_count<guess_most:
    if guess_count > 0:
        answer = input('Guess again')
    else:
        answer=input('Guess a number ')
    guess_count+=1
    if str(answer)==str(winning_number):
        print('Correct!')
        break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what function should i use to tidy up my code (extreme beginner) scraig0117 4 2,278 Dec-16-2019, 04:03 PM
Last Post: scraig0117
  Beginner problem, replace function with for loop Motley_Cow 9 4,605 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  recursion function (python beginner) everafter 3 2,875 Aug-19-2019, 07:24 AM
Last Post: buran
  Error in using the output of one function in another function (beginner) MadsPJ 6 5,019 Mar-13-2017, 03:06 PM
Last Post: MadsPJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020