python 这一直在第54行给我一个语法错误,但我真的搞不清楚它是什么

krugob8w  于 2023-02-21  发布在  Python
关注(0)|答案(2)|浏览(145)
print("Use this program alongside a turnip forecaster")
price = input("Enter turnip buying price")
amt = input("Enter amount of turnips bought")
total = int(price) * int(amt)

monmin = input("Enter Monday minimum value here [-->]")
tuesmin = input("Enter Tuesday minimum value here [-->]")
wedmin = input("Enter Wednesday minimum value here [-->]")
thursmin = input("Enter Thursday minimum value here [-->]")
frimin = input("Enter Friday minimum value here [-->]")
satmin = input("Enter Saturday minimum value here [-->]")

mins = {"Monday min": int(monmin), 
        "Tuesday min": int(tuesmin), 
        "Wednesday min": int(wedmin), 
        "Thursday min": int(thursmin), 
        "Friday min": int(frimin), 
        "Saturday min": int(satmin)
}
monmax = input("Enter Monday max value here [-->]")
tuesmax = input("Enter Tuesday max value here [-->]")
wedmax = input("Enter Wednesday max value here [-->]")
thursmax = input("Enter Thursday max value here [-->]")
frimax = input("Enter Friday max value here [-->]")
satmax = input("Enter Saturday max value here [-->]")

maxes = {"Monday max": int(monmax), 
        "Tuesday max": int(tuesmax), 
        "Wednesday max": int(strwedmax), 
        "Thursday max": int(thursmax), 
        "Friday max": int(frimax), 
        "Saturday max": int(satmax)
}

monform_min = mins["Monday min"] * amt
tuesform_min = mins["Tuesday min"] * amt
wedform_min = mins["Wednesday min"] * amt
thursform_min = mins["Thursday min"] * amt
friform_min = mins["Fridaymin"] * amt
satform_min = mins["Saturdaymin"] * amt

monform_max = maxes["Mondaymax"] * amt
tuesform_max = maxes["Tuesday max"] * amt
wedform_max = maxes["Wednesday max"] * amt
thursform_max = maxes["Thursday max"] * amt
friform_max = maxes["Friday max"] * amt
satform_max = maxes["Saturday max"] * amt
print("Buying price = " + str(total))

print("""
Monday:
  Minimum selling value: """ + monform_min """
  Minimum net value: """ + monform_min - total """
  
  Maximum selling value: """ + monform_max """
  Maximum net value: """ + monform_max - total
)

input('press 1 to see tuesday')

print("""Tuesday:
  Minimum selling value: """ + tuesform_min """
  Minimum net value: """ + tuesform_min - total """
  
  Maximum selling value: """ + tuesform_max """
  Maximum net value: """ + tuesform_max - total
)

input('press 1 to see wednessday')

print("""wednesday:
  Minimum selling value: """ + wedform_min """
  Minimum net value: """ + wedform_min -= total """
  
  Maximum selling value: """ + wedform_max """
  Maximum net value: """ + wedform_max -= total
)

input('press 1 to see thursday')

print("""Thursday:
  Minimum selling value: """ + thursform_min """
  Minimum net value: """ + thursform_min -= total """
  
  Maximum selling value: """ + thursform_max """
  Maximum net value: """ + thursform_max -= total
)

input('press 1 to see tuesday')

print("""Friday:
  Minimum selling value: """ + friform_min """
  Minimum net value: """ + friform_min -= total """
  
  Maximum selling value: """ + friform_max """
  Maximum net value: """ + friform_max -= total
)

input('press 1 to see saturday')

print("""Saturday:
  Minimum selling value: """ + satform_min """
  Minimum net value: """ + satform_min -= total """
  
  Maximum selling value: """ + satform_max """
  Maximum net value: """ + satform_max -= total
)

我试图从一个等式中减去一个数字,它给了我一个语法错误,它一直说我需要添加更多细节,所以它在字符串的第54行,但由于它是一个多行引号,它没有指出错误的确切位置,我不知道,它仍然说它需要更多细节,这就足够了,现在怎么样?

jexiocij

jexiocij1#

第54行似乎是以下语句中的第三行:

print("""
Monday:
  Minimum selling value: """ + monform_min """        # <-- this one.
  Minimum net value: """ + monform_min - total """
  
  Maximum selling value: """ + monform_max """
  Maximum net value: """ + monform_max - total
)

有没有什么原因导致您在该行的最后一行"""之前省略了+以及其他一些行?我非常肯定您需要它们:-)
顺便提一下,一个更好的方法是重构代码以实现可重用性,类似于:

def print_day(day, mini, maxi, total):
    print(f"{day}:")
    print(f"  Minimum selling value: {mini}")
    print(f"  Minimum net value:     {mini - total}")
    print(f"  Maximum selling value: {maxi}")
    print(f"  Maximum net value:     {maxi - total}")

# And call with the following:

print_day("Monday", monform_min, monform_max, total);
print_day("Tuesday", tueform_min, tueform_max, total);
# ... and so on

这将:

  • 让你的代码更具可读性;以及
  • 确保任何bug只在一个地方修复,而不是在所有你剪切和粘贴代码的地方修复。

还有其他改进的机会,例如,每当你看到一组 * 不同 * 的项/代码段,除了一个次要属性/代码动作之外,它们几乎是相同的,你应该想到“列表”(或者其他一些集合)。
对列表进行迭代,对每个元素做同样的事情,将比一长串重复的代码节给予更简洁、可读和 * 可维护 * 的代码。

mf98qq94

mf98qq942#

正如@Tim Roberts评论的那样,f字符串会让你的print语句更简洁:

print(f"""
Monday:
  Minimum selling value: {monform_min}
  Minimum net value: {monform_min - total}
  
  Maximum selling value: {monform_max}
  Maximum net value: {monform_max - total}"""
)

相关问题