My While loop isn't working in python -


my while loop not repeating though sentry value has not been met break loop. please help.the while loop looks me should not break until varaiable computernumber equal number

import random print("hello , welcome ai guessing game pick number 1 100 press h or l give clue") number=input("pick number 1 100") number=int(number) tries = 0 computernumber=1 while computernumber !=(number):     computernumber=random.randint(1,100)     print (computernumber)     higherorlower=input("is number higher or lower use h or l please!")     if higherorlower == "h":         computernumber +=1         tries = int(tries)         tries +=1         computernumber=random.randint (computernumber,number)     else:             computernumber -=1             computernumber=random.randint (1,computernumber)             tries = 0             tries = int(tries)             tries +=1             print (computernumber)              break  if computernumber == number:      if tries == 1 or 0:          print("your number was", number,"and took me",tries,"try")     else:         print("your number was", number,"and took me",tries,"tries")  

  1. computernumber=random.randint(1,100) take random number in range(1,100) in every iteration.. thats why should not put inside loop

  2. break in else break loop. thats why loop not repeating.

modified code:

import random print("hello , welcome ai guessing game pick number 1 100 press h or l give clue") number=input("pick number 1 100 :") number=int(number) tries = 0 computernumber=1  # bring outside of loop. computernumber=random.randint(1,100)  while computernumber !=(number):     print (computernumber)     higherorlower=input("is number higher or lower use h or l please!")     if higherorlower == "h":         computernumber +=1         tries = int(tries)         tries +=1         computernumber=random.randint (computernumber,number)     else:             computernumber -=1             computernumber=random.randint (1,computernumber)             tries = 0             tries = int(tries)             tries +=1              # not use break here             #break  if computernumber == number:     if tries == 1 or 0:          print("your number was", number,"and took me",tries,"try")     else:         print("your number was", number,"and took me",tries,"tries") 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -