[Tutor] python help

Alan Gauld alan.gauld at yahoo.co.uk
Sat Sep 12 13:06:22 EDT 2020


On 12/09/2020 16:35, trista rayment via Tutor wrote:
> I need to write a code that asks for a number between 5 and 25 

> print('Enter an integer between 5 and 25')howMany = input()

OK, you seem to have that bit OK, although I assume the fact it is all
on one line is an email format wobble due to you not posting in plain text?

The next bit is to convert howMany to a number
(are floats allowed or is it just integers?)

> and only allows integers 
> if it types a number less than 5 or more than 25,

> limit(int(num, minimum = 5, maximum = 25)    else ValueError

Now this makes no sense. limit is not a python builtin function.
int() does not take min or max range values and ValueError needs
to be "raised" Also you don;t have a variable called 'num'...

Something like this:

try:
   howMany = int(howMany)   # this raises the ValueError for you
except ValueError:
   print(<something here>)

# now check the range:
if howMany not in range(5,26):
   print(<something else here>)

> it needs to re-ask the original question
> until it gets an appropriate answer

The repetition implies some kind of loop.
Which is better a for or a while?
Which is used when you don;t know how many loops you need to do?


> if ValueError =     print('Enter an integer between 5 and 25')

This is not how to test for Valueerror - see above

> for howMany in range (5, 25)    print(....

This will set howMany to each value in the range.
Thats not really what you want!

> while howMany = (>=5, <=25):    continue

That makes no sense and is not valid Python.

> I can't seem to get to any usable code that makes sense to even me. 

I've given you some starting points above, see f you can put it together
to get closer to what you want. If not come back with your new code.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list