学校计划无效

时间:2016-11-16 14:47:52

标签: python

我是一个python noob,在学校我需要编写一个有6个选项的程序:

  1. 查找平方根
  2. 平均考试成绩
  3. 显示税款
  4. 随机性
  5. 查找日期和今天的天数
  6. 并退出
  7. 除日期外,我完成了所有这些工作。这是我的代码:

       #test.py
        from math import *
        from random import *
        from datetime import *
        cont = True
        a = 0
        print("My Custom Functions")
        print("1. What's the root?")
        print("2. Average my test scores.")
        print("3. Show me the tax.")
        print("4. Randomness.")
        print("5. How many days?")
        print("6. Exit")
    
        def option():
         cont = True
         global a
         global cont
         o = input("Enter an option")
         if o == "1":
            a = float(input("This function prints a square root. Enter a number (1-9999):"))
            print(sqrt(a))
        if o == "2":
            b = input("Enter any number of test scores (1-100). Separate each by a space:")
            l = []
        l = [float(score) for score in b.split()]
        avg =sum(l) / len(l)
        print(round(avg,5))
        if o == "3":
            c = float(input("This function shows how much your sales tax (7.5%) is for your purchase. Enter the purchase amount (<10,000):"))
            d = c * .075
            print("$",c+d)
        if o == "4":
         print(randrange(1,100))
        if o == "5":
            d = input("“This function shows how many days there are between today and a date you enter. Enter a date in this format (mm dd yy):")
            int(d)
            d1 = date(d)
            d2 = date.today()
            print(d2 - d1)
        if o == "6":
            print("Exitting Program")
            cont = False
    
        while cont == True:
        option()
    

    我编辑了代码作为我收到的答案。我现在得到ValueError:int()的无效文字,基数为10:'10 24 18'。我已经研究过一个没有运气的解决方案。

2 个答案:

答案 0 :(得分:1)

对于选项2,在分割字符串后转换为float

b = input("Enter any number of test scores (1-100). Separate each by a space:")
l = [float(score) for score in b.split()]
print(sum(l) / len(l))

对于选项5,您需要正确使用datetime.date个对象。

d = input("“This function shows how many days there are between today and a date you enter. Enter a date in this format (mm dd yy):")
mm, dd, yy = [int(t) for t in d.split()]
d1 = date(2000 + yy, mm, dd)
print((date.today() - d1).days)

答案 1 :(得分:0)

空格分隔float的列表不是float,您需要先将其拆分,然后将它们转换为浮点数:

if o == "2":
    b = input("Enter any number of test scores (1-100). Separate each by a space:")
    l = list(map(float, b.split()))  # or use a list comprehension
    print(sum(l) / len(l))

而且您无法将日期转换为int,这是没有意义的。