INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

how to prevent user from typing a value like 1
2

how to prevent user from typing a value like 1

how to prevent user from typing a value like 1

(OP)
I am working on this code, how can i make it better?
The first think i want to make better is change it so if a value like 1 or something is entered it will say
you can only enter a value A to U

CODE --> python

grade = input("What is your grade?")
if grade == "A":
	print ("A definitely stands for AMAZING!")
elif grade == "B":
	print( "That’s really good well done!")
elif grade == "C" :
	print ("You Passed your exam!")
else:
	print("I think you might need some revision lessons!") 

RE: how to prevent user from typing a value like 1

Using [link https://docs.python.org/2/library/functions.html#r...]raw_input('prompt text') may be better, but you cannot limit what a user van actually enter in response to a simple input() function. That has to be done after you have get the input and check what has been entered.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: how to prevent user from typing a value like 1

(OP)
I am having logic problems with this
when I type in for example B or b or c or C etc. it prints
The new grading for A* is a value 8

please help

CODE --> python

grade = input("What is your grade?")
if grade in ("A*", "a*", "A", "a", "B", "b", "C", "c"):
        if grade == "A*" or "a*":
                print ("The new grading for A* is a value 8")
        elif grade == "A" or "a":
                print ("The new grading for A is a value 7")
        elif grade == "B" or "b":
                print ("The new grading for B is a value 6")
        elif grade == "C" or "c":
                print ("The new grading for C is a value 5")
        else:
                print("I think you might need some revision lessons!")
else:
        print(grade, "is not a real grade") 

RE: how to prevent user from typing a value like 1

Hi

CODE

if grade == "A*" or "a*":
#  ╰─────────┬─╯    ╰┬─╯
#            │       │
# if either this or this is true
#            │        ╰────╮
# ╭──────────┴────────╮  ╭─┴──────────────────╮
# │ true if the two   │  │ true if the string │
# │ strings are equal │  │ is not empty       │
# ╰───────────────────╯  ╰────────────────────╯ 

Instead of separate comparison for uppercase and lowercase versions of a string, better transform the input into uppercase, then compare only that :

CODE --> Python

grade = input("What is your grade?").upper()
grade_dict = {
    'A*': 8,
    'A': 7,
    'B': 6,
    'C': 5,
}
if grade in grade_dict:
    print("The new grading for", grade, "is a value", grade_dict[grade])
else:
    print(grade, "is not a real grade") 

Feherke.
feherke.ga

RE: how to prevent user from typing a value like 1

(OP)
feherke
This code works brilliantly
thank you
As mentioned I am a newbie to python, is this an array or switch or something in between.

RE: how to prevent user from typing a value like 1

Hi

Quote (69ashton)

is this an array or switch or something in between.
There I used a dictionary, also referred as dict in Python documentations. It is like associative arrays in other languages.

When started with Python, I found the Dictionaries section of Mark Pilgrim's Dive Into Python 3 to be and easy and efficient explanation.

Feherke.
feherke.ga

RE: how to prevent user from typing a value like 1

(OP)
OMG!
Thank you feherke what a great site, I also love dictionaries (i have used it some more) , great data structure!

RE: how to prevent user from typing a value like 1

as a minor alternative to Feherke's code (Python prefers the "it is easier to ask for forgivnes than permission" philosophy as opposed to "Look Before you Leap")

CODE

grade = input("What is your grade?").upper()
grade_dict = {
    'A*': 8,
    'A': 7,
    'B': 6,
    'C': 5,
}
try:
    print("The new grading for", grade, "is a value", grade_dict[grade])
except KeyError:
    print(grade, "is not a real grade") 
Not much of a change really


Do things on the cheap & it will cost you dear

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close