Input Validation # between 1-10 and no letters

Page 1 of 1

4 Replies - 1921 Views - Last Post: 29 November 2011 - 01:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257678&amp;s=2a4c5779493af241db3aadce22d277a8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 wooleryk

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 29-November 11

Input Validation # between 1-10 and no letters

Posted 29 November 2011 - 12:14 PM

```def romanConversion():
roman = 0
print 'Enter a number between 1 and 10 and I will convert it to a Roman Numeral'
roman = raw_input ('Enter your number: ')
while not roman.isdigit():
print 'You did not enter a number between 1 and 10!  Try again!'
roman = raw_input ('Enter a number between 1 and 10 please!')
roman = int(roman)
if roman == 1:
print 'The roman numeral conversion for 1 is I'
if roman == 2:
print 'The roman numeral conversion for 2 is II'
if roman == 3:
print 'The roman numeral conversion for 3 is III'
if roman == 4:
print 'The roman numeral conversion for 4 is IV'
if roman == 5:
print 'The roman numeral conversion for 5 is V'
if roman == 6:
print 'The roman numeral conversion for 6 is VI'
if roman == 7:
print 'The roman numeral conversion for 7 is VII'
if roman == 8:
print 'The roman numeral conversion for 8 is VIII'
if roman == 9:
print 'The roman numeral conversion for 9 is XI'
if roman == 10:
print 'The roman numeral conversion for 10 is X'

```

With the code I have now I can enter a letter and it will give me an error. If I enter a number greater than 10 the program will still run and I dont want it to do that. I really have not idea how to make it flag both.
Ive tried google searches but have had not luck in trying to figure it out. Any help in the right direction would be greatly appreicated

This post has been edited by Motoma: 29 November 2011 - 12:39 PM
Reason for edit:: Fixed your [code] tags.

Is This A Good Question/Topic? 0

Replies To: Input Validation # between 1-10 and no letters

#2 Motoma

Reputation: 452
• Posts: 798
• Joined: 08-June 10

Re: Input Validation # between 1-10 and no letters

Posted 29 November 2011 - 12:41 PM

You a small change to your while statement to include a check for roman > 10 would do the trick.

#3 wooleryk

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 29-November 11

Re: Input Validation # between 1-10 and no letters

Posted 29 November 2011 - 01:03 PM

tried various options

while not roman.isdigit() and roman >11:
or
while not roman.isdigit() or roman >11:
or
while not roman.isdigit()not roman >=1 and <=10

I've tired a few other combinations but it still will let a value such as 45 through or other instances where it will tell me that say I enter the number 3 that it is still invalid. I see that I need to add something but I am not able to make anything work. Thanks for the response though.

#4 Motoma

Reputation: 452
• Posts: 798
• Joined: 08-June 10

Re: Input Validation # between 1-10 and no letters

Posted 29 November 2011 - 01:20 PM

Sorry I didn't notice this before. The reason your attempts failed is because you convert to an integer after the while statement. At the point of the while statement, roman is still a string, meaning that your comparisons will be alphabetical, rather than numerical. For example:
```>>> '4' < '5'
True
>>> '4' > '5'
False
>>> '45' > '5'
False

```

What you need to do is move line 8 up before the while statement, then you can add your comparisons to the while statement in order to make this all work properly.

A minor correction: you will still need roman to be a string in order to perform the isdigit() check. Something along these lines would work, however:

```while (not roman.isdigit()) or (int(roman) < 1) or (int(roman) > 10):

```

Note, I've used parentheses to make the logic easier to recognize. When starting out, it is better to make such things explicit rather than working through Python's order of operations.

#5 wooleryk

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 29-November 11

Re: Input Validation # between 1-10 and no letters

Posted 29 November 2011 - 01:39 PM

Thank you that finally made it work. Using the parentheses made it a lot easier. I kept trying to do it without the () and couldn't get it to work. I still had to keep line 8 where it was though roman = int (roman).

Thanks again.