我为什么会收到这个 ValueError?

huangapple go评论72阅读模式
英文:

Why am i getting this ValueError

问题

对于 k 在范围内的 no_of_resc:
    name_resch = input('输入您想重新安排的患者的姓名:')
    date_resc = input('输入您想重新安排的日期:')
    date_for_resc = datetime.datetime.strptime(date_resc, '%Y-%m-%d %H:%M:%S')
    print('预约已重新安排为', name_resch, ',日期为', date_for_resc)
    resch_pat.append(name_resch)
print('预约已重新安排为:', resch_pat)

现在这是源代码。请有人帮我处理一下这个。

现在这是输出。在这里我有6个选项(忽略那些选项),当我输入我想要预约的日期时,我得到了一个类似于 ValueError 的错误。

英文:

now this is the source. Someone please help me how to deal with this

for k in range(no_of_resc):
                name_resch = input('enter then name of the patient you want to reschedule : ')
                date_resc = input('enter the date you want to reschedule : ')
                date_for_resc = datetime.datetime.strptime(date_resc, '%Y-%m-%d %H:%M:%s')
                print('the appointment is rescheduled for ',name_resch,'and on',date_for_resc)
                resch_pat.append(name_resch)
            print('the appointment is rescheduled for :' , resch_pat)

now now this is the output So here i have 6options (forget about those option) when i enter the date i want to get appointment im getting this error something like ValueError

1 . register no.of  doctors 
2 . register no.of patients 
3 . do you want to book appointments ? 
4 . reschedule the appointments ?      
5 . cancel the appointments. 
6 . Exit 
enter your selection : 3 
enter the number of doc : 1 
enter number of patient : 1 
>> 
enter the name of the doctor : knan 
is the doctor free? (y/n) : y 
[]
['y ']
enter the name of the patient : ganesh 
['ganesh ']
lol
enter the date you want to get appointed (yyyy-mm-dd hh:mm): 2023-10-9 8:31:31 
Traceback (most recent call last):
  File "c:\Desktop\vsc\JavaScript-Guess-My-Number\starter\doctor.py", line 51, in <module>
    date = datetime.datetime.strptime(appnt_date,'%Y-%m-%d %H:%M')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\_strptime.py", line 568, in _strptime_datetime        
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\_strptime.py", line 352, in _strptime
    raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: :31 

答案1

得分: 1

在这一行中,您输入带有秒数的日期:

输入您想要预约的日期(yyyy-mm-dd hh:mm:ss):2023-10-9 8:31:31

但是,使用此模式进行转换是不正确的,在这里您有(c:\Desktop\vsc\JavaScript\05-Guess-My-Number\starter\doctor.py):

date = datetime.datetime.strptime(appnt_date, '%Y-%m-%d %H:%M')

转换应该是:

date = datetime.datetime.strptime(appnt_date, '%Y-%m-%d %H:%M:%s')
英文:

In this line, you enter the date with seconds:

enter the date you want to get appointed (yyyy-mm-dd hh:mm): 2023-10-9 8:31:31

But the conversion is not correct with this pattern, you have here (c:\Desktop\vsc\JavaScript\05-Guess-My-Number\starter\doctor.py):

date = datetime.datetime.strptime(appnt_date,'%Y-%m-%d %H:%M')

The conversion should be:

date = datetime.datetime.strptime(appnt_date,'%Y-%m-%d %H:%M:%s')

huangapple
  • 本文由 发表于 2023年1月9日 16:26:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定