运行我的代码时,我不断收到错误(TypeError:字符串索引必须是整数,而不是str)

时间:2017-11-05 12:19:01

标签: python string

我尝试运行下面的代码,我希望在我选择日期和时间时显示每日特价但我似乎得到错误(字符串索引必须是整数,而不是str)当我运行代码。任何帮助表示赞赏。

def get_specials():

    monday = {"B": "Horseradish omelet. Note: better than it sounds",
              "L": "Momma's Curry. Note: Can be made spicy.",
              "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"}
    tuesday = {"B": "Sausage gravy over biscuits. Note: Toast can be subbed",
              "L": "Grilled cheese and tomato soup. Note: We have vegan cheese",
              "D": "Meatloaf. Note: Comes woth catsup on the top. Not optional"}
    wednesday = {"B": "Horseradish omelet. Note: better than it sounds",
                 "L": "Momma's Curry. Note: Can be made spicy.",
                 "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"}
    thursday = {"B": "Sausage gravy over biscuits. Note: Toast can be subbed",
                "L": "Grilled cheese and tomato soup. Note: We have vegan cheese",
                "D": "Meatloaf. Note: Comes woth catsup on the top. Not optional"}
    friday = {"B": "Horseradish omelet. Note: better than it sounds",
              "L": "Momma's Curry. Note: Can be made spicy.",
              "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"}
    saturday = {"B": "Sausage gravy over biscuits. Note: Toast can be subbed",
                "L": "Grilled cheese and tomato soup. Note: We have vegan cheese",
                "D": "Meatloaf. Note: Comes woth catsup on the top. Not optional"}

    specials = {"M": "monday",
                "T": "tuesday",
                "W": "wednesday",
                "R": "thursday",
                "F": "friday",
                "St": "saturday"}
    return specials

def print_special(special):
    print "The special is:"
    print special
    print "*"*15

def get_day():
    while True:
        day = raw_input ("Day (M/T/W/R/F/St): ")
        if day.upper() in ["M", "T", "W", "R", "F", "ST"]:
            return day.upper()
        else:
            print "I'm sorry, but {} isn't valid".format(day)

def get_time():
    while True:
        time = raw_input ("Time (B/L/D): ")
        if time.upper() in ["B", "L", "D"]:
            return time.upper()
        else:
            print "I'm sorry, but {} isn't valid".format(time)

def main():
    specials = get_specials()
    print "This script will tell you the specials for any day and time of the week"
    while True:
        day = get_day()
        special = specials[day]
        time = get_time()
        print_special(special[time])
# ********************^^^^ HERE ^^^*
        another = raw_input("Do you want to check another day and time?(Y/N): ")
        if another.lower == "n":
            break

main()

星号行是问题所在。 (星号不是代码的一部分,它只是突出显示特定行的错误)显示的错误如下。

Traceback (most recent call last):
  line 62, in <module>
    main()
  line 57, in main
    print_special(special[time])
TypeError: string indices must be integers, not str 

2 个答案:

答案 0 :(得分:1)

错误是因为strings会返回一个字典,其中的键和值均为string而不是键dict和值specials = {"M": monday, "T": tuesday, "W": wednesday, "R": thursday, "F": friday, "St": saturday} 。所以将specials字典更改为

#include "device.h"

答案 1 :(得分:0)

问题在于:

print_special(special[time])

例如:

假设您的词典是:

monday = {"B": "Horseradish omelet. Note: better than it sounds",
              "L": "Momma's Curry. Note: Can be made spicy.",
              "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"}

我们想要&#39; B&#39;的价值所以你会这样做:

print(monday['B'])

但是你在做:

print('monday'['B'])

您可以查看:

day = get_day()
special = specials[day]
time = get_time()
print(type(special))
print(time)

输出中:

monday
<class 'str'>
B

建议你的代码: 您不需要额外的功能只是打印结果我已经修改了您的代码一点结帐:

def get_specials(day,time):
    special_dish = {"monday": {"B": "Horseradish omelet. Note: better than it sounds",
                      "L": "Momma's Curry. Note: Can be made spicy.",
                      "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"},
           "tuesday": {"B": "Sausage gravy over biscuits. Note: Toast can be subbed",
                       "L": "Grilled cheese and tomato soup. Note: We have vegan cheese",
                       "D": "Meatloaf. Note: Comes woth catsup on the top. Not optional"},
           "wednesday": {"B": "Horseradish omelet. Note: better than it sounds",
                         "L": "Momma's Curry. Note: Can be made spicy.",
                         "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"},
           "thursday": {"B": "Sausage gravy over biscuits. Note: Toast can be subbed",
                        "L": "Grilled cheese and tomato soup. Note: We have vegan cheese",
                        "D": "Meatloaf. Note: Comes woth catsup on the top. Not optional"},
           "friday": {"B": "Horseradish omelet. Note: better than it sounds",
                      "L": "Momma's Curry. Note: Can be made spicy.",
                      "D": "Beef brisket. Note: COmes with au jus. That's pronounced 'Oh jhoo', not 'Ow Juice'"},
           "saturday": {"B": "Sausage gravy over biscuits. Note: Toast can be subbed",
                        "L": "Grilled cheese and tomato soup. Note: We have vegan cheese",
                        "D": "Meatloaf. Note: Comes woth catsup on the top. Not optional"}}

    specials = {"M": "monday",
                "T": "tuesday",
                "W": "wednesday",
                "R": "thursday",
                "F": "friday",
                "St": "saturday"}
    get_day=specials.get(day)
    get_special=special_dish.get(get_day).get(time)
    return "The special is: {}".format(get_special)



def get_day():
    while True:
        day = input ("Day (M/T/W/R/F/St): ")
        if day.upper() in ["M", "T", "W", "R", "F", "ST"]:
            return day.upper()
        else:
            print ("I'm sorry, but {} isn't valid".format(day))

def get_time():
    while True:
        time = input ("Time (B/L/D): ")
        if time.upper() in ["B", "L", "D"]:
            return time.upper()
        else:
            print ("I'm sorry, but {} isn't valid".format(time))

def main():

    print ("This script will tell you the specials for any day and time of the week")
    while True:
        day = get_day()
        time = get_time()
        print(get_specials(day,time))

# ********************^^^^ HERE ^^^*
        another = input("Do you want to check another day and time?(Y/N): ")
        if another.lower == "n":
            break

main()

输出:

This script will tell you the specials for any day and time of the week
Day (M/T/W/R/F/St): M
Time (B/L/D): B
The special is:Horseradish omelet. Note: better than it sounds
Do you want to check another day and time?(Y/N):