为计算机科学做一些编码作业,但不能为我的生活弄明白

时间:2017-11-09 13:03:31

标签: python python-3.x

#Alrighty lets give this a shot.
#So it no longer works and i can't figure out why....

coach = 550
entry = 30
students = 45
maxstudents = 45
discount = 0
moneydiscount = 0
cost = 0
studentcost = 0

Run = True

while Run == True:

    students = int(input("Please input number of students going on the trip:"))
    if students > 45 or students <=0:
        print("Wrong number of students detected. Please consult your Principal or try again.")
    elif students < 45:
        print("Number of students =")
        print(students)
        print("The cost per student will be:")
        ticket_cost = (students * 30)
        num_free_tickets = int(students / 10)
        moneydiscount = (num_free_tickets * 30)
        cost = str(round(coach + ticket_cost - moneydiscount, 2))
        studentcost = str(round(5 + cost / students, 2))
        profit = (students * 5)
        #this makes sure it is in payable amounts^
        print(student_cost)
        print("profit is:")
        print(profit)
    else:
        print("This input is not numerical, please only insert numerical values")

它给了我这个错误: Traceback(最近一次调用最后一次):   文件&#34; C:\ Users \ littl \ Desktop \ Folders \ Home-Ed Work \ Computer Science \ Python Homework Post-Summer \ 06-11-17 \ Students.py&#34;,28行in     studentcost = str(round(5 + cost / students,2)) TypeError:/:&#39; str&#39;不支持的操作数类型和&#39; int&#39;

所以我知道它与: studentcost = str(round(5 + cost / students,2))

但是因为上面的线几乎是相同的并且让我感到茫然...

5 个答案:

答案 0 :(得分:2)

尝试使用int()费用和学生。例如int(cost)。问题是学生和成本是字符串类型,这与没有强制转换的int不兼容。

答案 1 :(得分:2)

str()创建一个文本字符串,你无法对其进行数学运算。例如,您需要一个整数类型。在你的情况下,你已经拥有:

cost = round(coach + ticket_cost - moneydiscount, 2)
studentcost = round(5 + cost / students, 2)

如果您的文字包含数字,则可以将其转换为:

some_value = int("1")

答案 2 :(得分:1)

高级答案:这是一个类型问题,可以通过将变量转换为可以在数学中使用的int或float来解决。

由于这是家庭作业我不会只是给你答案。但我发现了两个问题。首先尝试添加类型打印语句。

    cost = str(round(coach + ticket_cost - moneydiscount, 2))
    print("student: ", type(students))
    print("cost:", type(cost))
    student_cost = str(round(5 + (cost) / students, 2))

1)那应该揭示你的问题所在(它对我有用)。你需要在某处添加3个小字母。

2)修复后的第二个,您可能需要检查所有变量名称的一致性。修复第28行问题后,您会注意到错误。尝试并消化你得到的错误。

  

/ str'和'int'

的操作数类型不受支持

/(除)操作数/操作在字符串和int之间不起作用...现在看一下打印调试语句。如果您需要其他帮助,请与我们联系。

答案 3 :(得分:0)

是Python是动态的,它是最好的,以便将int转换为float或int to long,但是当你将str添加到int(&#34; 1&#34; + 2)时,有无法判断你是否希望输出为字符串&#34; 12&#34;或者int 3, 因此,在这种情况下,有意做出了例外的决定。 在您的代码中,您不需要将每个中间结果转换为str。只需按原样将其打印出来..

答案 4 :(得分:0)

这有效:

coach = 550
entry = 30
students = 45
maxstudents = 45
discount = 0
moneydiscount = 0
cost = 0
studentcost = 0

Run = True

while Run == True:

    students = int(input("Please input number of students going on the trip:"))
    if students > 45 or students <=0:
        print("Wrong number of students detected. Please consult your Principal or try again.")
    elif students < 45:
        print("Number of students =")
        print(students)
        print("The cost per student will be:")
        ticket_cost = (students * 30)
        num_free_tickets = int(students / 10)
        moneydiscount = (num_free_tickets * 30)
        cost = str(round(coach + ticket_cost - moneydiscount, 2))
        studentcost = round(5 + float(cost)/float(students), 2)
        profit = (students * 5)
        #this makes sure it is in payable amounts^
        print(studentcost)
        print("profit is:")
        print(profit)
    else:
        print("This input is not numerical, please only insert numerical values")