How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask ATLPROG Your Own Question
ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 9151
Experience:  MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
44910485
Type Your Programming Question Here...
ATLPROG is online now
A new question is answered every 9 seconds

How can I get this python script to only accept numbers 0-10

This answer was rated:

How can I get this python script to only accept numbers 0-10 for rating input?



def main():
name=raw_input('Enter the name: ')
while name =='':
print'Error in input. Name cannot be blank.'
name=raw_input('Try again. Enter the name: ')

rating=raw_input('Enter the evilness rating: ')

while rating.isdigit == False:
print'Error in input.Rating must be a non-negative integer.'
rating=raw_input('Try again. Enter the evilness rating: ')

rating =int(rating)
laugh=' Bwa'+'ha'*int(rating)
print 'My name is %s.%s.'%(name,laugh)

main()

ATLPROG :

Hi Welcome to Just Answer, It will be my pleasure to help you.

Customer:

Hi

ATLPROG :

Hello

ATLPROG :

What version of Python are you using ?

ATLPROG :

2.6 ?

Customer:

2.7

ATLPROG :

k

ATLPROG :

and can you please help me understand what are you writing this program for ?

Customer:

homework

ATLPROG :

I mean can you give me your assignment or homework details

Customer:

It's quite lengthy.I need to write a program that accepts a name but will not accept a blank input for name.It needs to accept a user input rating from 0-10. Then it either returns a statement that multiplies the laugh# XXXXX the rating number or an error message. It all works except the rating must be limited to 0 to 10 inclusive.

ATLPROG :

ok...

ATLPROG :

Pythin code on the question text is not formatted well

ATLPROG :

can you send me your .py file ?

ATLPROG :

Can you please go to www.wikisend.com and upload your file ? Once you do that you will get a page that has the "File ID" and "Download Link". Copy the "Download Link" or "File ID" and please paste it in your reply here.

Customer:

Can't do that. I have to go to work. Thought this might be an easy way to get a simple answer to my homework question.

ATLPROG :

yes, but your code is not formatted very well...and python is very much space sensitive

ATLPROG :

Let me give it a shot regardless then, if you don't want to upload code

ATLPROG :

try this ...

ATLPROG :

rating=int(raw_input('Enter the evilness rating: '))

ATLPROG :

that should work

ATLPROG :

let me know what you find

Customer:

That will not help at all. I need to limit the numbers that can be input to 1 -10

ATLPROG :

ok...

ATLPROG :

didn't realise you need to do that

ATLPROG :

CAn I write code to prompt user to re-enter if its a invalid number ?

Customer:

If it's an invalid number it should say "Sorry that rating is invalid"

ATLPROG :

ok..

ATLPROG :

working on adding that code

Customer:

My teacher suggests a loop sentinel, but not necessary.

ATLPROG :

k

ATLPROG :

I have this ready

ATLPROG :

let me send you a link

ATLPROG :

Please download the program from this Answer_Link

ATLPROG :

Let me know if you still have questions

ATLPROG :

There is a RAW Paste data in section below the formatted section on link I gave you, which should help

Customer:

There is a link answerlink but I can't find any information

ATLPROG :

can you click on that link please ?

ATLPROG :

WHat do you see when you open that link ?

ATLPROG :

Let em paste it here in this chat

ATLPROG :


def main():

name=raw_input('Enter the name: ')

while name =='':

print'Error in input. Name cannot be blank.'

name=raw_input('Try again. Enter the name: ')

rating=raw_input('Enter the evilness rating: ')

while rating.isdigit == False:

print'Error in input.Rating must be a non-negative integer.'

rating=raw_input('Try again. Enter the evilness rating: ')

rating =int(rating)


while rating > 10 or rating < 1:

print'Sorry that rating is invalid.'

rating=raw_input('Try again. Enter the evilness rating: ')

rating =int(rating)


laugh=' Bwa'+'ha'*int(rating)

print 'My name is %s.%s.'%(name,laugh)

main()

ATLPROG :

there you go

ATLPROG :

Did it help ?

ATLPROG :

???

ATLPROG :

you still with me ?

Customer:

yes, but I can't seem to figure out the formatting.

Customer:

When I click on the link if something comes up it;s all java

ATLPROG :

well thats why I pasted you the code above

ATLPROG :

Did that work ?

Customer:

No because I can't figure out the indenting.

ATLPROG :

Well now you know why I was asking you for a python file my friend !

ATLPROG :

Anyways, Try this LINK

ATLPROG :

Python code should be there

ATLPROG :

let me know if you still have issues

ATLPROG :

Did it work ???

Customer:

Sorry but it totally doesn't work now. I f you run the program it gives me an error message for any rating number now. I do know that the rating changes must be done within the same while loop to work. Like I said my teacher suggests a loop sentinel.

ATLPROG :

hmm...are you sure you are running my program ?

Customer:

Yes. Copy and paste.

ATLPROG :

Let me give you a different link

ATLPROG :

Try this LINK

ATLPROG :

Wait a second, I see the problem...why it works some times and why it does not

Customer:

I realize that programmers don't usually use idle, but there must be some way to try out the script.If you enter Bob for the name and 5 for the rating. It should print My name isXXXXX Insead I recieve the error message using your script.

ATLPROG :

ok...Its because of different version of Python...

ATLPROG :

This should work with Python 2.7

ATLPROG :

Try this LINK

Customer:

Okay, it works great now, BUT it still does not respond correctly if you enter alpha characters into the rating. It should only accept numbers 0 through 10 or return an error message. That's why I believe it cannot be accomplished with 2 separate while statements.

ATLPROG :

let me look

ATLPROG :

Try this LINK

Customer:

Can you tell me what you did. I don't see the difference. My teacher said 2 separate while loops would not work but it does. And if it works I get the credit. This was the only assignment I couldn't figure out this quarter. thanks.

ATLPROG :

any luck ?

ATLPROG :

I am nto sure why your teacher would say two separate while loops won't work

Customer:

It's good, bit can you explain the last change?

ATLPROG :

I don't have all your 'requirement's or task details, but from what you ahve told me, it works

ATLPROG :

yes I can

ATLPROG :

I am using rating.isdigit() method to find out if its a digit

ATLPROG :

and this method works only on strings

ATLPROG :

not on ints

ATLPROG :

so the conversion to integer should happen after this check is complete

ATLPROG :

We can do this with one while loop too , if thats what you would like

ATLPROG :

but the messages may get tricky to handle

Customer:

No, it's good.

ATLPROG :

cool

Customer:

Thanks we are done.

ATLPROG :

I hope you get good grade

ATLPROG :

Please start your question with "For ATLProg only" if you would need any help on Programming related questions in future.

ATLPROG :

Please remember to hit the Green accept button if you think my answer helped, so that I get paid for the time spent. I would obviously continue to help you if you have any further questions.

Customer:

You may be hearing from me.

ATLPROG and other Programming Specialists are ready to help you
Customer: replied 5 years ago.
After thoroughly trying the script, it still doesn't work.If you make a couple incorrect answers first-such as,15 or dog in the rating, it will then return with the correct input such as 6 as incorrect. So it STILL does not work. Can you fix it?
Tamera
SO what are the inputs that it does not work for ? Can you please provide them more clearly ?

Can you send me the data values you are testing it with. I will take a look
Customer: replied 5 years ago.
Here's the output of the script.It seems to work just fine and then it crashes,at about the third input.
Is that enough information?
Tamera

Enter the name: Bobby
Enter the evilness rating: 46
Sorry, rating must be between 0 and 10, inclusive.
Try again. Enter the evilness rating: -15
Sorry, rating must be between 0 and 10, inclusive.
Try again. Enter the evilness rating: cat

Traceback (most recent call last):
File "C:\Users\Tamera\Desktop\lab7_input_validation.py", line 29, in <module>
main()
File "C:\Users\Tamera\Desktop\lab7_input_validation.py", line 24, in main
rating =int(rating)
ValueError: invalid literal for int() with base 10: 'cat'
>>>
Please download the program from this Answer_Link