d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Question > Sentinel Value
Add Reply New Topic New Poll
Member
Posts: 28,924
Joined: Aug 25 2006
Gold: 13.00
Trader: Trusted
Mar 25 2018 06:06pm
So my brother has a homework assignment and I was trying to help him figure it out.

Essentially the parameters are as follows.


We have been able to get everything to work except the "x". I had the thought that perhaps the instructor was actually wanting a random value of our choice to be used as opposed to an actual x, but its not clear. Using x always returns an error because the program wants an integer and we haven't figured out any other ways to make this work.

Any assistance is greatly appreciated.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Mar 25 2018 07:35pm
Code
% ./foo.py
Please enter x between 1 and 100: 50
Close, but no cigar! The number is in range (50, 100)
Please enter x between 50 and 100: 75
Close, but no cigar! The number is in range (50, 75)
Please enter x between 50 and 75: 63
Close, but no cigar! The number is in range (50, 63)
Please enter x between 50 and 63: 56
Close, but no cigar! The number is in range (56, 63)
Please enter x between 56 and 63: 59
Close, but no cigar! The number is in range (59, 63)
Please enter x between 59 and 63: 61
Close, but no cigar! The number is in range (59, 61)
Please enter x between 59 and 61: 60
Bravo! You've guessed correctly!


Code
% cat foo.py
#!/usr/bin/python

from random import randrange;

def get_num(min, max):
while(True):
x = int(input("Please enter x between %d and %d: " % (min, max)));
if(x > max or x < min):
print("Inorrect value!")
continue

return x


min, max = 1, 100
guessed = randrange(min, max)

while(True):
x = get_num(min, max)
if(x == guessed):
print("Bravo! You've guessed correctly!")
break;
elif(x > guessed):
max = x
elif(x < guessed):
min = x

print("Close, but no cigar! The number is in range (%d, %d)" % (min, max))


This post was edited by KrzaQ2 on Mar 25 2018 07:46pm
Member
Posts: 48,295
Joined: Jul 5 2008
Gold: 792.00
Mar 25 2018 07:48pm
Quote (KrzaQ2 @ 25 Mar 2018 19:35)
Code
% ./foo.py
Please enter x between 1 and 100: 50
Close, but no cigar! The number is in range (50, 100)
Please enter x between 50 and 100: 75
Close, but no cigar! The number is in range (50, 75)
Please enter x between 50 and 75: 63
Close, but no cigar! The number is in range (50, 63)
Please enter x between 50 and 63: 56
Close, but no cigar! The number is in range (56, 63)
Please enter x between 56 and 63: 59
Close, but no cigar! The number is in range (59, 63)
Please enter x between 59 and 63: 61
Close, but no cigar! The number is in range (59, 61)
Please enter x between 59 and 61: 60
Bravo! You've guessed correctly!


Code
% cat foo.py
#!/usr/bin/python

from random import randrange;

def get_num(min, max):
while(True):
x = int(input("Please enter x between %d and %d: " % (min, max)));
if(x > max or x < min):
print("Inorrect value!")
continue

return x


min, max = 1, 100
guessed = randrange(min, max)

while(True):
x = get_num(min, max)
if(x == guessed):
print("Bravo! You've guessed correctly!")
break;
elif(x > guessed):
max = x
elif(x < guessed):
min = x

print("Close, but no cigar! The number is in range (%d, %d)" % (min, max))


Lol, I knew you could do it in your sleep bud! 😂😂😂
Member
Posts: 28,924
Joined: Aug 25 2006
Gold: 13.00
Trader: Trusted
Mar 25 2018 07:58pm
Thanks I will check this out tomorrow. Sadly he ran out of time and had to turn in but still we wanted to know what we did wrong. I had a feeling it was going to involve the elif function but in my testing I never could get it to work.
Member
Posts: 28,924
Joined: Aug 25 2006
Gold: 13.00
Trader: Trusted
Mar 26 2018 09:59am
So I took a look at this and it's not actually correct. The entry range is not supposed to change, it's always a value between 1 and 100. To quit the program you must enter x but again this returns the same error we've been encountering because it expects an integer and x isn't. So essentially what we need is a way of making it terminate with x and ignore that it's not an integer. It's also supposed to keep a running total of the integer entries. For example, if you enter 5 it returns 5 and then after that if you enter 10 it returns 15, 20 returns 35, etc.
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
Mar 26 2018 11:27am
Compare input with "x" and exit if needed, if it's not x proceed and THEN cast it to int.
Member
Posts: 48,295
Joined: Jul 5 2008
Gold: 792.00
Mar 26 2018 11:48am
Member
Posts: 28,924
Joined: Aug 25 2006
Gold: 13.00
Trader: Trusted
Mar 27 2018 10:44am
Finally got this figured out with much research and a small amount of help from reddit community. I'm pretty happy as I am a self taught beginner and this was my first actual project. Thanks for the help guys.

Code
#Variables
total = 0

#Conditions
while True:
guess = input('Please enter a number between 1 and 100 or enter "x" to quit.')
try:
number = int(guess)
if number > 1 and number < 100:
total += number
print(total)
elif number <= 1 or number >= 100:
print('Invalid entry. Please try again. You must enter a number between 1 and 100 or "x" to quit.')
except ValueError:
if guess == "x":
break
elif guess != "x":
print('Invalid entry. Please try again. You must enter a number between 1 and 100 or "x" to quit.')
continue
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll