2017-05-01 10 views
0

我的代码是这样的:在蟒蛇在日历中输入字符串而不是整数的

import sys 
import calendar 

trueVal = 'yes' 
while trueVal == 'yes' : 

yy = int(input("Enter year: ")) #input should be a string 
mm = int(input("Enter month: ")) 

print(calendar.month(yy, mm))#returns the result 

cmd=input("Would you like to view another calendar? Type yes if you do, no to exit program") 


if cmd != trueVal : 
    sys.exit() 

它的实际工作,但我的教授说 “修复它尝试输入的不是整数的字符串。” , 所以我试图更换intstr 知道我是一个臭名昭着的初学者,它给了我一个错误(我期待不过)。

我得到了一些建议,说输出必须转换为str,我该怎么做?

+0

你使用Python 2.7? – garg10may

+0

所以我澄清了我的教授abt她所要求的。她甚至想让我做一个额外的代码行,如果输入不是整数,则表示错误。例如,如果用户输入一个整数。 –

+0

哦,即时通讯使用python 3.5 –

回答

-1

你可以试试下面的代码:

import sys 
import calendar 

trueVal = 'yes' 

while trueVal == 'yes': 
    yy = input("Enter year: ") #input should be a string 
    mm = input("Enter month: ") 
    print(calendar.month(int(yy), int(mm)))#returns the result 
    cmd = input("Would you like to view another calendar? Type yes if you do, no to exit program") 

    if cmd != trueVal: 
     sys.exit() 

您的意见将是字符串,并浇铸成函数内部诠释。

-1

enter image description here Calendar.month方法只接受整数,但您要给出字符串值。

1

使用的不是int()自身的功能:

def myint(s): 
    try: 
    return int(s) 
    except: 
    print("Input '%s' does not look like integer") 
    sys.exit(0)