How to write an input asking the user to type using both letters and numbers?

How to write an input asking the user to type using both letters and numbers?



I am writing a line where I ask the user to input their school ID, which requires both letters and numbers. How can I do this? When I input a number as my answer when asked, my program works. If I type a letter with the number I get the error.
Here is my code thus far using Python 3.6 IDLE: def main():


def main():


#Introduction
print ("Welcome to my program!n")

#Variables with starting values waiting for user input

studentid = ""
gpa = 0.0
classnumber = ""

#Prompt for studentID
studentid = eval(input("Please enter your student ID: "))
#Prompt for GPA
gpa = eval(input("Please enter your GPA: "))
#Prompt for classnumber
classnumber = eval(input("Please enter your class information:"))

#display the users information
#StudentID: C100556789
#GPA: 4.0
#Class: COP1000

print ("nStudent ID:t", studentid)
print ("GPA:t", gpa)
print ("Class information:t", classnumber)



And here is my error:


Type "copyright", "credits" or "license()" for more information.
>>> RESTART: C:UsersRene DelgadoDocumentsSchoolFall 2018School CodeStudent Program.py
>>> main() Welcome to my program!

Please enter your student ID: C100556789 Traceback (most recent call
last): File "<pyshell#0>", line 1, in <module>
main() File "C:UsersRene DelgadoDocumentsSchoolFall 2018School CodeStudent Program.py", line 18, in main
studentid = eval(input("Please enter your student ID: ")) File "<string>", line 1, in <module> NameError: name 'C100556789' is not
defined




1 Answer
1



input returns a string that the user inputs, which is what you want already. You should not use eval to evaluate the string as a Python expression, which would generate an error for an input with letters because Python would interpret it as a variable name that does not exist. Simply remove calls to eval and your code would work:


input


eval


eval


#Prompt for studentID
studentid = input("Please enter your student ID: ")
#Prompt for GPA
gpa = input("Please enter your GPA: ")
#Prompt for classnumber
classnumber = input("Please enter your class information:")



Note that if you want to treat gpa as a number for numeric operations, you should also convert the input to a floating number with float():


gpa


float()


gpa = float(input("Please enter your GPA: "))





This worked! Thank you for the help. This was my first post on Stack, you guys rock.
– Rene Delgado
Sep 1 at 3:11



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế