简体   繁体   English

如何重复我的循环并将更多信息添加到我的字典中,并使用不同的字符串?

[英]How can I repeat my loop and add more information to my dictionary, and use different strings?

I am trying to create a program so that I can input all the shifts I work in a week, and it will calculate the total number of hours I work, and how much I money I will make. 我正在尝试创建一个程序,以便我可以输入我一周工作的所有班次,它将计算我工作的总小时数,以及我将赚多少钱。

I have created a dictionary called week_schedule and an input prompt to ask the user what day of the week they are going to log. 我创建了一个名为week_schedule的字典和一个输入提示,询问用户他们要记录的星期几。 The program then asks the shift start and finish, creating variables for both of them, and storing it all in a dictionary. 程序然后询问班次的开始和结束,为它们创建变量,并将它们全部存储在字典中。

How can I rerun this loop, so that I can input more than one day, and store it all in a dictionary that I can then print out later? 如何重新运行此循环,以便我可以输入超过一天,并将其全部存储在字典中,然后我可以打印出来? Currently rerunning the loop just adds more information to the same strings. 目前重新运行循环只会向相同的字符串添加更多信息。

week_schedule = {}

    day = input("Day of the week: ")

    day_promp = True

    while day_promp:
        dayStart = input("Shift Start: ")
        dayEnd = input("Shift End: ")

        dayHours = (float(dayEnd) - float(dayStart)) / 100
        dayPay = (float(dayHours) * 15.75)

        week_schedule[day] = format(dayHours) + " hours", "$ " + format(dayPay)

        repeat = input("\nMore days?: ")
        if repeat == 'no':
            day_promp = False

print("\n--- Week summary ---")
for day, dayHours in week_schedule.items():
    print(format(day) + ": " + format(dayHours))
    print("Total earned: $" + format(dayPay))

Currently I can run the program and it works for only one day. 目前我可以运行该程序,它只工作一天。 For example if I imputed 'Tuesday" for the day, "1200" for the shift start, and "1800" for the shift end, it would output the following: 例如,如果我将当天的“星期二”估算为“星期二”,班次开始为“1200”,班次结束为“1800”,则输出以下内容:

--- Week summary ---
Tuesday: ('6.0 hours', '$ 94.5')
Total earned: $94.5
--------------------

I would like to be able to print out all the days I input in the dictionary as well. 我希望能够打印出我在字典中输入的所有日子。

Move your day = input("Day of the week: ") inside the loop, so it will be 在循环中移动你的day = input("Day of the week: ") ,这样就可以了

week_schedule = {}
grand_total = 0
day_promp = True

while day_promp:
    # Do it each iteration
    day = input("Day of the week: ")
    dayStart = input("Shift Start: ")
    dayEnd = input("Shift End: ")

    dayHours = (float(dayEnd) - float(dayStart)) / 100
    dayPay = (float(dayHours) * 15.75)
    # sum up all the totals
    total += dayPay  

    week_schedule[day] = format(dayHours) + " hours", "$ " + format(dayPay)

    repeat = input("\nMore days?: ")
    if repeat == 'no':
        day_promp = False

print("\n--- Week summary ---")
# Now your dict has a key per day, so all values are printed.
for day, dayHours in week_schedule.items():
    print(format(day) + ": {} ".format(dayHours))
    print("Total earned: $ {0:.2f}".format(dayPay))

print("Grand Total earned $ {0:.2f}".format(total))

Your original code assigns day = input("Day of the week: ") only once, that's why you're always getting only one value. 您的原始代码仅指定day = input("Day of the week: ")一次,这就是为什么您总是只获得一个值。

As mentioned in the comments above, the easiest way to make a program like this would be to have it write to a file in a given format. 正如上面的评论中所提到的,制作这样的程序最简单的方法就是让它以给定的格式写入文件。 For example: 例如:

WagePerHour = 9.50
Mode = input("Would you like to log another day, clear the log, or see the current total?  ")
if Mode == "log":
    EnterMore = True
    while EnterMore == True:
        Day = input("What day of the week are you logging?  ")
        Hours = input("How many hours did you work?  ")
        Days = { "Sunday" : "1", "Monday" : "2", "Tuesday" : "3", "Wednesday" : "4", "Thursday" : "5", "Friday" : "6", "Saturday" : "7"}
        File = open("File", "a")
        DayAsNumber = Days[Day]
        File.write(DayAsNumber + " " + Hours)
        File.write("\r\n")
        File.close()
        continuevar = input("Would you like to log another day? (Y/N)  ")
        if continuevar != "Y":
            EnterMore = False
if Mode == "total":
    File = open("File", "r")
    TotalHours = 0
    for line in File.readlines():
        if line[2:] != "":
            TotalHours += int(line[2:])
            TotalHoursString = str(TotalHours)
            print("So far this week, you have logged " + TotalHoursString + " hours.")
            File.close()
            TotalHoursAsInt = int(TotalHoursString)
            TotalWages = (TotalHoursAsInt * WagePerHour)
            TotalWagesString = str(TotalWages)
            print("Your wages before taxes are " + TotalWagesString)
        else:
            print("You have not logged any hours this week.")
if Mode == "clear":
    File = open("File", "w")
    File.write("")
    File.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符添加到字典的字符串中? - How can I add a character to the strings of my dictionary? 如何使用mw字典添加新的字符串列表的分子量? - How I can add the molecular Weight of my new list of strings using a mw dictionary? 如何从列表或字典中向子图添加标题? - How can I add titles to my subplots from a list or dictionary? 如何通过循环将变量添加到字典中? - How can I add variables into a dictionary with a loop? 如何让 python 使用更多的 CPU? - How can I make python use more of my cpu? 我如何让我的字典从我的正则表达式循环中打印出更多的值,而不是只打印最后一个值? - How do I get my dictionary to print out more values from my regex loop instead of printing only the last one? 如何将带有 if 语句的 for 循环的结果添加到我的 dataframe? - How can I add results of a for loop with an if statement to my dataframe? 我如何才能在阅读 csv 文件的字典中获得更多解决方案? - How can i do to get more solutions in my dictionary reading an csv file? 如何在我的代码中添加两个不同的图像? - how can I add two different images to my code? 如何让我的代码循环重复? - How do I get my code to repeat in a loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM