python 3.x - How to use a while loop to make code continue? -


sorry, tried researching while loops , examples found didn't me much. having hard time understanding concept outside of peoples examples. new python , tutorials use while loop in different scenario. here code:

# guess number game. import random  # ask user name print ('hello. name?') name = input ()  # ask user if play game. # if user confirms, game continues # if user denies, game ends print ('hi ' + name + ' nice meet you.') print ('would play game me?')  answer = input() confirm = ['yes', 'yes',' y', 'y', 'yea', 'yea', 'yeah', 'yeah', 'yup',  'yup'] deny = ['no', 'no', 'n', 'n', 'nope', 'nope', 'nah', 'nah']  if answer in confirm:   print ('great! let\'s started!') elif answer in deny:   print ('i sorry hear that. maybe next time? goodbye') + exit()  print ('i thinking of number between 1 , 20.')   print ('can guess number is?')   secretnumber = random.randint (1, 20) print('debug: secret number ' + str(secretnumber)) # debug  guessestaken in range (1, 7):   print ('take guess.')   guess = int(input())    if guess < secretnumber:     print ('your guess low.')   elif guess > secretnumber:     print ('your guess high!')   else:     break # condition correct guess!  if guess == secretnumber:   print ('good job, ' + name + '! guessed number in ' +       str(guessestaken) + ' guesses.') else:   print ('wrong. number thinking of ' + str(secretnumber))  print ('would play again?') play_again = input()  if play_again in confirm:     print('# put code make game restart')  elif play_again in deny:     print ('thanks playing!')  exit()     

i use while loop (because think thats need, please enlighten me if not) @ "if play_again in confirm:" statement make return "i thinking of number between 1 , 20" line. way user can continue play game if choose.

thankyou in advance. using newest python.

your code while loop added:

# guess number game. import random  # ask user name print ('hello. name?') name = input ()  # ask user if play game. # if user confirms, game continues # if user denies, game ends print ('hi ' + name + ' nice meet you.') print ('would play game me?')  answer = input() confirm = ['yes', 'yes',' y', 'y', 'yea', 'yea', 'yeah', 'yeah', 'yup',  'yup'] deny = ['no', 'no', 'n', 'n', 'nope', 'nope', 'nah', 'nah']  if answer in confirm:   print ('great! let\'s started!') elif answer in deny:   print ('i sorry hear that. maybe next time? goodbye') + exit()  while true:    print ('i thinking of number between 1 , 20.')     print ('can guess number is?')     secretnumber = random.randint (1, 20)   print('debug: secret number ' + str(secretnumber)) # debug    guessestaken in range (1, 7):     print ('take guess.')     guess = int(input())      if guess < secretnumber:       print ('your guess low.')     elif guess > secretnumber:       print ('your guess high!')     else:       break # condition correct guess!    if guess == secretnumber:     print ('good job, ' + name + '! guessed number in ' +       str(guessestaken) + ' guesses.')   else:     print ('wrong. number thinking of ' + str(secretnumber))    print ('would play again?')   play_again = input()    if play_again in confirm:     #print('# put code make game restart')      continue   elif play_again in deny:     print ('thanks playing!')     break  exit()  

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -