₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,252 members, 8,420,986 topics. Date: Friday, 05 June 2026 at 04:05 PM

Toggle theme

Help With This Code (python) - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHelp With This Code (python) (1450 Views)

1 Reply (Go Down)

Help With This Code (python) by Nobody: 7:28pm On May 10, 2016
This is a very simple code...
i need some help with it.

a=5
guess_a = int(input('enter an integer'))

if guess_a == a:
print('success'.upper())
elif guess_a < a:
print('failed..try again')
else:
print('failed..try again')

how can i make it keep running until the right integer is inputted?
Re: Help With This Code (python) by odeh1(m): 7:42pm On May 10, 2016
samted:
This is a very simple code... i need some help with it.
a=5 guess_a = int(input('enter an integer'))
if guess_a == a: print('success'.upper()) elif guess_a < a: print('failed..try again') else: print('failed..try again')
how can i make it keep running until the right integer is inputted?
use looping
Re: Help With This Code (python) by Nobody: 7:43pm On May 10, 2016
odeh1:
use looping
How do i implement the loop? And which one sef?
Re: Help With This Code (python) by Nobody: 8:24pm On May 10, 2016
Hey.

guessed=False; #variable to indicate if correct guess has been made
a = 5; #sets correct guess
while not(guessed): #while loop condition to check if correct guess has been made
guess = int(input('Enter an integer: '))
if (guess==a):
print('Success, well done.')
guessed=True; #sets guessed to true to break the loop
elif guess<a:
print('Try a higher number.')
else:
print("Try a lower number." )
Re: Help With This Code (python) by Nobody: 8:28pm On May 10, 2016
Nairaland has poor indenting features. Fixed.
Re: Help With This Code (python) by Nobody: 8:53pm On May 10, 2016
sonOfLucifer:
Hey.

guessed=False; #variable to indicate if correct guess has been made
a = 5; #sets correct guess
while not(guessed): #while loop condition to check if correct guess has been made
guess = int(input('Enter an integer: '))
if (guess==a):
print('Success, well done.')
guessed=True; #sets guessed to true to break the loop
elif guess<a:
print('Try a higher number.')
else:
print("Try a lower number." )
Ok..lemme try this
Re: Help With This Code (python) by Nobody: 9:02pm On May 10, 2016
sonOfLucifer:
Hey.

guessed=False; #variable to indicate if correct guess has been made
a = 5; #sets correct guess
while not(guessed): #while loop condition to check if correct guess has been made
guess = int(input('Enter an integer: '))
if (guess==a):
print('Success, well done.')
guessed=True; #sets guessed to true to break the loop
elif guess<a:
print('Try a higher number.')
else:
print("Try a lower number." )
Thanks..this worked fab. What if i wish to limit the number of attempts? To say maybe 5. Then the code stops executing...
Re: Help With This Code (python) by paparazzi1987(m): 9:23pm On May 10, 2016
samted:
Thanks..this worked fab. What if i wish to limit the number of attempts? To say maybe 5. Then the code stops executing...
Declare a counter value, and start incremental for each attempt
Re: Help With This Code (python) by Nobody: 9:26pm On May 10, 2016
paparazzi1987:
Declare a counter value, and start incremental for each attempt
Ermm...how? I just started learning this stuff so all this one na big grammar
Re: Help With This Code (python) by Nobody: 10:19pm On May 10, 2016
samted:
Thanks..this worked fab. What if i wish to limit the number of attempts? To say maybe 5. Then the code stops executing...

guessed=False #variable to indicate if correct guess has been made
a = 5 #sets correct guess
tries = 5
while not(guessed): #while loop condition to check if correct guess has been made

for i in range(tries): #for loop checks number of tries
guess = int(input('Enter an integer: '))
tries-=1
if (guess==a):
print('Success, well done.')
guessed=True
break
elif guess<a:
print('Try a higher number.')
else:
print('Try a lower number.')
print('Number of tries left: ' + str(tries))
if (tries==0):
print('Sorry, no tries left\n')

Re: Help With This Code (python) by khaynoni(m):

answer = 5
attempts = 5
while attempts > 0:
print('{0} attempt(s) left.'.format(attempts))
guess = int(input('Enter an integer: '))
if guess == answer:
print('Congrats! you guessed the answer correctly.')
break
elif guess < answer:
print('\nTry a higher number.\n')
else:
if attempts != 1:
print('\nTry a lower number.\n')
else:
print('Game over!!!')
attempts -= 1

Re: Help With This Code (python) by Nobody: 11:11pm On May 10, 2016
sonOfLucifer:

guessed=False #variable to indicate if correct guess has been made
a = 5 #sets correct guess
tries = 5
while not(guessed): #while loop condition to check if correct guess has been made

for i in range(tries): #for loop checks number of tries
guess = int(input('Enter an integer: '))
tries-=1
if (guess==a):
print('Success, well done.')
guessed=True
break
elif guess<a:
print('Try a higher number.')
else:
print('Try a lower number.')
print('Number of tries left: ' + str(tries))
if (tries==0):
print('Sorry, no tries left\n')

Thanks bruh. Perfect.
Re: Help With This Code (python) by Nobody: 11:56pm On May 10, 2016
Someone help me try this out grin
from time import sleep
print('let\'s play a game'.upper())
sleep(1.0)
print('it\'s a simple guessing game'.upper())
sleep(1.0)
print('you have 3 attempts'.upper())
sleep(1.0)

guessed = False
a = 5
tries = 3
while not(guessed):
for i in range(tries):
guess_a=int(input('enter an integer:'.upper()))
tries-=1
if guess_a==a:
sleep(0.5)
print('success'.upper())
guessed=True
break

elif guess_a>a:
sleep(0.5)
print('sorry..try a lower number')

else:
sleep(0.5)
print('sorry..try a higher number')
sleep(0.5)
print('Attempts remamining:'+str(tries))

if tries==0:
print('Sorry..no tries left')
quit()
sleep(1.0)
quit()
1 Reply

Can Someone Help Me Explain This Code Line?Java Programmers ,I Need Help Fixing This Code.Please Someone Should Help Me With This Code.234

Cyber Security Free IT SchoolXamarin Android/ios/forms Tutorials C# For BeginnersThe Complete Ethical Hacking Course- Biginner To Advanced!