我对编码(Python)还是很陌生,对于我的第一个项目,我正在制作一个小型调查程序,该程序根据用户的生日告诉他们他们的十二生肖。如果他们输入的是非整数或无效的日期(例如生日无效,请重试),然后重复一遍又一遍直到输入正确的内容,我想输入循环错误消息。问题是我不确定要使用哪个循环以及在哪里放置循环(最后?)
这是程序的代码:
print('Hello SDSA!')
print('What is your name?')
myname = input()
print('Nice to meet you, ' + myname + '!')
print('How old are you?')
myage = input()
print('Amazing. Could you tell me your birthday?' + '(Please input your birthday in mmdd format.)')
bd = input()
if int(bd) in range(321,332) or int(bd) in range(401,421):
print("You're a " + myage + "-year-old Aries!")
zod = 'Aries'
elif int(bd) in range(421,431) or int(bd) in range(501,522):
print("You're a " + myage + "-year-old Taurus!")
zod = 'Taurus'
elif int(bd) in range(522,531) or int(bd) in range(601,622):
print("You're a " + myage + "-year-old Gemini!")
zod = 'Gemini'
elif int(bd) in range(622,631) or int(bd) in range(701,723):
print("You're a " + myage + "-year-old Cancer!")
zod = 'Cancer'
elif int(bd) in range(723,731) or int(bd) in range(801,823):
print("You're a " + myage + "-year-old Leo!")
zod = 'Leo'
elif int(bd) in range(823,832) or int(bd) in range(901,923):
print("You're a " + myage + "-year-old Virgo!")
zod = 'Virgo'
elif int(bd) in range(923,931) or int(bd) in range(1001,1023):
print("You're a " + myage + "-year-old Libra!")
zod = 'Libra'
elif int(bd) in range(1023,1031) or int(bd) in range(1101,1122):
print("You're a " + myage + "-year-old Scorpio!")
zod = 'Scorpio'
elif int(bd) in range(1122,1131) or int(bd) in range(1201,1222):
print("You're a " + myage + "-year-old Sagittarius!")
zod = 'Sagittarius'
elif int(bd) in range(1222,1231) or int(bd) in range(101,121):
print("You're a " + myage + "-year-old Capricorn!")
zod = 'Capricorn'
elif int(bd) in range(121,131) or int(bd) in range(201,220):
print("You're a " + myage + "-year-old Aquarius!")
zod = 'Aquarius'
elif int(bd) in range(220,230) or int(bd) in range(301,321):
print("You're a " + myage + "-year-old Pisces!")
zod = 'Pisces'
答案 0 :(得分:1)
例如,如果答案是数字,则在python函数中内置了somehere.isdigit()例如:
myage = input()
if myage.isdigit() == True:
print("Its a number")
else:
print("Its not a number")
关于日期发布
import datetime
while True:
myage = input()
try:
datetime.datetime.strptime(myage, '%Y-%m-%d')
### anything to do after like checking zodiacs######
break
except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DD")
答案 1 :(得分:0)
您需要使用datetime strptime函数进行验证,该函数可以接收字符串并将其与所需的datetime格式进行比较。在此CSE中,%m%d表示2位数的月份和2位数的日期(前导零)。
您可以创建一个while循环,直到满足条件为止。在这种情况下,如果输入格式不正确,valid_db行将失败并引发ValueError。传递此异常,然后再次启动循环。如果strptime()函数成功(它们以您想要的方式输入日期),则break语句将退出循环并继续。
import datetime
while True:
try:
bd = input('Amazing. Could you tell me your birthday?' + '(Please input your birthday in mmdd format.)')
valid_db = datetime.datetime.strptime(bd, '%m%d')
break
except ValueError:
pass
答案 2 :(得分:0)
好工作。尝试将代码放入while loop
中,如下所示:
print('Amazing. Could you tell me your birthday?' + '(Please input your birthday in mmdd format.)')
while True:
bd = input()
if int(bd) in range(321,332) or int(bd) in range(401,421):
print("You're a " + myage + "-year-old Aries!")
zod = 'Aries'
break
elif int(bd) in range(421,431) or int(bd) in range(501,522):
print("You're a " + myage + "-year-old Taurus!")
zod = 'Taurus'
break
# The rest of your code goes here
else:
print("Invalid birthday, please try again")
别忘了在正确的回答后面加上break
语句,否则您将陷入infinite loop的境地。
玩得开心!
答案 3 :(得分:0)
这是一种方式
print('Hello SDSA!')
print('What is your name?')
myname = input()
print('Nice to meet you, ' + myname + '!')
print('How old are you?')
myage = input()
print('Amazing. Could you tell me your birthday?',)
invalid_birthday = True
while invalid_birthday:
invalid_birthday = False
print('(Please input your birthday in mmdd format.)')
bd_input = input()
bd = int(bd_input) if bd_input.isdigit() else 0
if bd in range(321,332) or bd in range(401,421):
print("You're a " + myage + "-year-old Aries!")
zod = 'Aries'
elif bd in range(421,431) or bd in range(501,522):
print("You're a " + myage + "-year-old Taurus!")
zod = 'Taurus'
elif bd in range(522,531) or bd in range(601,622):
print("You're a " + myage + "-year-old Gemini!")
zod = 'Gemini'
elif bd in range(622,631) or bd in range(701,723):
print("You're a " + myage + "-year-old Cancer!")
zod = 'Cancer'
elif bd in range(723,731) or bd in range(801,823):
print("You're a " + myage + "-year-old Leo!")
zod = 'Leo'
elif bd in range(823,832) or bd in range(901,923):
print("You're a " + myage + "-year-old Virgo!")
zod = 'Virgo'
elif bd in range(923,931) or bd in range(1001,1023):
print("You're a " + myage + "-year-old Libra!")
zod = 'Libra'
elif bd in range(1023,1031) or bd in range(1101,1122):
print("You're a " + myage + "-year-old Scorpio!")
zod = 'Scorpio'
elif bd in range(1122,1131) or bd in range(1201,1222):
print("You're a " + myage + "-year-old Sagittarius!")
zod = 'Sagittarius'
elif bd in range(1222,1231) or bd in range(101,121):
print("You're a " + myage + "-year-old Capricorn!")
zod = 'Capricorn'
elif bd in range(121,131) or bd in range(201,220):
print("You're a " + myage + "-year-old Aquarius!")
zod = 'Aquarius'
elif bd in range(220,230) or bd in range(301,321):
print("You're a " + myage + "-year-old Pisces!")
zod = 'Pisces'
else:
invalid_birthday = True
print ("Invalid birthday, please try again.")
答案 4 :(得分:-1)
您可以将循环放在开始。
while True:
print('Hello SDSA!')
print('What is your name?')
myname = input()
print('Nice to meet you, ' + myname + '!')
print('How old are you?')
myage = input()
print('Amazing. Could you tell me your birthday?' + '(Please input your birthday in mmdd format.)')
bd = input()
if int(bd) in range(321,332) or int(bd) in range(401,421):
print("You're a " + myage + "-year-old Aries!")
zod = 'Aries'
# rest of your code here...
else:
print ('Invalid Inputs. Please try again')