Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

numbers - python - how do I find two desired integer values in a variable?

I'm in the process of learning how to code in Python and have been doing numerous tasks to help solidify info.

I had 1 issue - I am creating code that will take the date (dd/mm/yyyy) and then the program will validate the data (i.e checking if there are errors in the field and if so, naming the error type; if no errors are found, the user is told the input was correct) - just some data validation practise.

Where I'm getting stuck is just assigning the variables - I have typed and tested the day and year correctly but I cant manage to get the 4th and 5th integer of the variable date for the month variable.

Here is my code that I am first producing to make sure the integers I will be working with are correct:

date = input("Please enter the date in format dd/mm/yyyy: ")
valid_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-'

#defines where to locate day in user input
dayDate = str(date)[:2]
# could also be written as dayDate = str(date)[:-8]

#defines where to locate month in user input
def month(date):
    return date[(len(date)//2)]
finalMonth = month(date)

#defines where to locate year in user input
yearDate = str(date)[-4:]


print(yearDate)
print(finalMonth)
print(dayDate)
  • From this code, the variables yearDate and dayDate present me with the values I want but the finalMonth is where i'm getting stuck. I've looked all over Google but can't seem to find the solution. If you do know how to solve my issue, I would really appreciate it if you could send the proper way to go about this and why you did what, as I am still kind of a newb in Python :)

  • I know the error is the function I've created for finding the month values, but that's precisely where I need help.

Thank you!

EDIT:

Sorry! I am new to Stack overflow so I didn't know.

so the code:

def month(date):
    return date[(len(date)//2)]
finalMonth = month(date)

print(finalMonth)

returns the output '/' but what I am trying to get is for example you input '26/01/2021' and the variable finalMonth will give '01' - this code produces '/'. I am not sure what method should be used to get the month value.

question from:https://stackoverflow.com/questions/65906122/python-how-do-i-find-two-desired-integer-values-in-a-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use split() to create a list from input as below

date = input("Please enter the date in format dd/mm/yyyy: ")
valid_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-'

#defines where to locate day in user input
day, month, year = date.split("/")
print( day, month, year )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...