Python Forum
Error when running the second time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when running the second time
#1
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
Reply
#2
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")
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "cannot identify image file" part way through running hatflyer 0 615 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Error when running kivy on python janeik 8 1,926 Jun-16-2023, 10:58 PM
Last Post: janeik
  Plotting by Time, error mansoorahs 1 707 May-16-2023, 09:46 AM
Last Post: Larz60+
  Getting error when running "MINUS" between 2 databases marlonbown 4 1,217 Nov-10-2022, 05:49 AM
Last Post: deanhystad
Question Running an action only if time condition is met alexbca 5 1,262 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Error while running code on VSC maiya 4 3,549 Jul-01-2022, 02:51 PM
Last Post: maiya
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,321 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Error when running a matplot lib example aurelius_nero 3 6,689 Apr-24-2022, 01:24 PM
Last Post: Axel_Erfurt
  Keep getting Session management error when running imshow in pycharm pace 0 2,061 Mar-25-2021, 10:06 AM
Last Post: pace
  Error when running script on startup in Linux NoahTheNerd 0 1,926 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020