0

The point of the script is to tell you what day of the week it will be after 'X' amount of days from a given day.

I've been learning how to code for a few hours so bear with me.

    # Prompt the user for input
print("This scipt is useful in finding out what day of the week it \
will be after 'X' ammount of days in relation to the current date.")
sunday = Sunday = 0
monday = Monday = 1
tuesday = Tuesday = 2
wednesday = Wednesday = 3
thursday = Thursday = 4
friday = Friday = 5
saturday = Saturday = 6
print(" ")
todaysDate = input("Enter todays date: ")
daysFromToday = int(input("Enter a integer for days from today: "))


# Compute equasion

daysFromDate = ((todaysDate + daysFromToday) % 7)


# Display results
print(" ")
print("Today is", todaysDate, "and in", daysFromToday, "days it will be", daysFromDate)
print("  ")
print("Sunday = 0, \
Monday = 1, \
Tuesday = 2, \
Wednesday = 3, \
Thursday = 4, \
Friday = 5,   \
Saturday = 6")
input("Press enter to close: ")

My error is

Traceback (most recent call last):
  File "C:\Desktop\daysfromdate.py", line 18, in <module>
    daysFromDate = ((todaysDate + daysFromToday) % 7)
TypeError: Can't convert 'int' object to str implicitly

I fixed by using

todaysDate = eval(input("Enter todays date: "))

now my script runs fine, but last question is how do I make it answer with the word version of the variables?

Current output is

Today is 0 and in 100 days it will be 2

I would like it to be

Today is Sunday in 100 days it will be Tuesday
4

2 回答 2

0

我认为您使用的是 Python 3.x?如果是这样,那么

todaysDate = input("Enter todays date: ")

将返回一个字符串 ( str) 对象,您应该像对daysFromToday. IE,

todaysDate = int(input("Enter todays date: "))

(在较旧的 Python 版本中,input会为您执行此转换。)

编辑:好的,我明白你在做什么。请考虑不要为此使用eval,因为这eval是一个非常不安全的结构,允许用户执行任意代码。更清洁的解决方案是:

weekdays = {"sunday": 0,
            "monday": 1,
            ...
            "saturday": 6}
dayoftheweek = weekdays[input("Enter the day of the week: ")]
于 2013-02-10T17:47:04.047 回答
0

您的代码实际上在我的 python 版本上运行,但是您的问题可能与您的变量“todaysDate”有关,您尚未将其转换为 int。尝试改变:

todaysDate = input("Enter todays date: ")

至:

todaysDate = int(input("Enter todays date: "))

看看是否有帮助。

于 2013-02-10T17:50:46.393 回答