为计算机科学做一些编码作业,但我一辈子都搞不懂

2024-10-03 13:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

#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")

它给了我这个错误: 回溯(最近一次呼叫): 文件“C:\Users\littl\Desktop\Folders\Home Ed Work\Computer Science\Python homeography Post-Summer\06-11-17\学生.py“,第28行,in studentcost=str(轮(5+成本/学生,2)) TypeError:不支持/:“str”和“int”的操作数类型

所以我知道这和: studentcost=str(轮(5+成本/学生,2))

但是因为上面这条线几乎是一样的,我不知所措。。。你知道吗


Tags: ofinputisitthis学生intprint
3条回答

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

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

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

some_value = int("1")

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

既然这是家庭作业,我就不给你答案了。但我发现了两个问题。首先尝试添加一个type print语句。你知道吗

    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行问题后,您将注意到错误。试着消化你所犯的错误。你知道吗

unsupported operand type(s) for /: 'str' and 'int'

/(除)操作数/操作在字符串和int之间不起作用…现在看看print debug语句。如果你需要更多的帮助,请告诉我。你知道吗

试着围绕成本和学生使用int()。例如int(cost)。问题是students和cost是string类型,这与没有强制转换的int不兼容。你知道吗

相关问题 更多 >