Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to prevent user from typing a value like 1 2

Status
Not open for further replies.

69ashton

Programmer
May 22, 2016
11
GB
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

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!")
 
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
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")
 
Hi

Code:
[b]if[/b] grade [teal]==[/teal] [green][i]"A*"[/i][/green] [teal]or[/teal] [green][i]"a*"[/i][/green]:
[gray]#  ╰─────────┬─╯    ╰┬─╯[/gray]
[gray]#            │       │[/gray]
[gray]# if either this or this is true[/gray]
[gray]#            │        ╰────╮[/gray]
[gray]# ╭──────────┴────────╮  ╭─┴──────────────────╮[/gray]
[gray]# │ true if the two   │  │ true if the string │[/gray]
[gray]# │ strings are equal │  │ is not empty       │[/gray]
[gray]# ╰───────────────────╯  ╰────────────────────╯[/gray]

Instead of separate comparison for uppercase and lowercase versions of a string, better [highlight]transform the input into uppercase[/highlight], then compare only that :
Python:
grade = input([green][i]"What is your grade?"[/i][/green])[highlight].upper()[/highlight]
grade_dict = {
    [green][i]'A*'[/i][/green]: 8,
    [green][i]'A'[/i][/green]: 7,
    [green][i]'B'[/i][/green]: 6,
    [green][i]'C'[/i][/green]: 5,
}
[b]if[/b] grade [teal]in[/teal] grade_dict:
    print([green][i]"The new grading for"[/i][/green], grade, [green][i]"is a value"[/i][/green], grade_dict[grade])
[b]else[/b]:
    print(grade, [green][i]"is not a real grade"[/i][/green])

Feherke.
feherke.ga
 
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.
 
Hi

69ashton said:
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
 
OMG!
Thank you feherke what a great site, I also love dictionaries (i have used it some more) , great data structure!

 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top