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: 8788
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

Resolved Question:

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()
Submitted: 3 years ago.
Category: Programming
Expert:  ATLPROG replied 3 years ago.

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, Computer Software Engineer
Category: Programming
Satisfied Customers: 8788
Experience: MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
ATLPROG and other Programming Specialists are ready to help you
Customer: replied 3 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
Expert:  ATLPROG replied 3 years ago.
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 3 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'
>>>
Expert:  ATLPROG replied 3 years ago.
Please download the program from this Answer_Link

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5603
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/unvadim/2010-11-15_210218_avatar.jpg unvadim's Avatar

    unvadim

    Computer Software Engineer

    Satisfied Customers:

    1158
    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    950
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    864
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/RA/rajivsharma086/2012-6-6_17128_displaypic.64x64.jpg Raj's Avatar

    Raj

    Computer Engg.

    Satisfied Customers:

    860
    BE CS, 4+ Experience in Programming and Database (ERP)
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    590
    11 yrs of programming (PHP, WordPress, XSL, SQL, JavaScript)
 
 
 

Related Programming Questions

Chat Now With A Programmer
ATLPROG
ATLPROG
Sr Software Engineer
7547 Satisfied Customers
MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML