Python Forum

Full Version: Error when running the second time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def check_id():
while True:
userid = input("Please enter your ID: ")
if len(userid) == 8 and ' 'not in userid and userid[0:2].isalpha()==True and userid[2:8].isdigit()==True:
print("Thanks for the input")
return userid
else:
print("Input must have only 8 characters and should not have whitespaces,the first 2 characters must be alphabets,please re-enter your ID")

def check_Registered():
while True:
Registration = input("Please provide your MRN/Have you submitted the forms? y/n: ")
if Registration == "y":
print("Thanks for the MRN/Thanks for submitting the forms")
return Registration
else:
print("Please register with us/Please submit the forms/C:\Forms\SubmitForm.pdf")
break

def check_name():
while True:
name = input("Please provide your name: ")
if len(name)!=0 and name.replace(" ","").isalpha() and name[0:1] is not ' ':
print("Thanks for the input")
return name
else:
print("The name entered is an empty string or has whitespaces or has characters other than alphabets,please re-enter your name")

def check_Doctor():
while True:
name = input("Please provide the name of the doctor you would like to book an appointment with: ")
if len(name)!=0 and name.replace(" ","").isalpha() and name[0:1] is not ' ':
print("Thanks for the input")
return name
else:
print("The name entered is an empty string or has whitespaces or has characters other than alphabets,please re-enter the Doctor's name")


def check_date():
from datetime import datetime
while True:
inputdate = input("Please enter date in DDMMYYY format: ")
ApptDateReq = datetime.strptime(inputdate, "%d/%m/%Y")
present = datetime.now()
if ApptDateReq.date() > present.date():
print("Thanks")
return inputdate
else:
print("You cannot book an appointment in the past,please re-enter date")

def check_phone():
while True:
phone = input("Please provide your phone number: ")
if all((len(phone) == 10, phone.isdigit())):
print("Thanks for the input,you have entered",phone)
return phone
else:
print("The phone number entered is an empty string or has whitespaces or has alphabets,please re-enter the phone number")

import datetime
from datetime import datetime
n = 5
while n > 0:
userid = check_id()
with open("C:\\Users\\ABC\\DEF\\" "%s.txt" % userid, "a") as f:
Registered = check_Registered()
name = check_name()
Doctor = check_Doctor()
ApptDate = check_date()
phone = check_phone()
EmailID = input("Please provide your email to confirm your appointment: ")
dtstamp = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
f.write(name+ "|" +Doctor+ "|" +ApptDate+ "|" +EmailID+ "|" +phone+ "|" +Registered+ "|" +dtstamp+ "\n")
MoreData = input("Do you need to make any new Appointments? y/n :")
if MoreData != "y":
n= -1


When I run the above code it runs fine the first time.
If I want to add more data and type y for Moredata = "y"
then it takes all the relevant input and the end after taking the email, it throws an error.
It creates the text file but does not add the data to the text file.

Error:
Please provide your email to confirm your appointment: [email protected]
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
TypeError: can only concatenate str (not "NoneType") to str

Any help is appreciated.
Thanks
One or more of your variables >name, Doctor, ApptDate, EmailID, phone, Registered, dtstamp< is None
because assumingly one or more functions donĀ“t return a string

f.write(name+ "|" +Doctor+ "|" +ApptDate+ "|" +EmailID+ "|" +phone+ "|" +Registered+ "|" +dtstamp+ "\n")
ThomasL,

I printed the type for all the returns in the functions and all of them are of type string.
Not sure what is going on.Will check again.
Thanks
You get an error if you do not answer "y" at MRN question because you break out of while loop
and then function check_Registered() returns None.
(as i already assumed)
def check_Registered():
    while True:
        Registration = input("Please provide your MRN/Have you submitted the forms? y/n: ") 
        if Registration == "y":
            print("Thanks for the MRN/Thanks for submitting the forms")
            return Registration
        else:
            print("Please register with us/Please submit the forms/C:\Forms\SubmitForm.pdf")
            break
            # check_Registered returns None
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-e22d5d7f577e> in <module> 73 EmailID = input("Please provide your email to confirm your appointment: ") 74 dtstamp = datetime.today().strftime('%Y-%m-%d %H:%M:%S') ---> 75 f.write(name + "|" + Doctor + "|" + ApptDate + "|" + EmailID + "|" + phone + "|" + Registered + "|" + dtstamp + "\n") 76 MoreData = input("Do you need to make any new Appointments? y/n :") 77 if MoreData != "y": TypeError: can only concatenate str (not "NoneType") to str