【问题标题】:Python Input and Output? [duplicate]Python 输入和输出? [复制]
【发布时间】:2021-11-02 17:47:23
【问题描述】:

我正在学习 python,但由于某种原因,我得到的输出总是不正确的。不知道我做错了什么或如何解决它,非常感谢任何帮助:

##a.    3 variables —
##  The name of a movie
##  The number of tickets to purchase
##  The total cost of tickets purchased
##
##b.        1 Constant —
##  The cost of a single ticket
##2.    Create four print statements that will display the values of the variables and the constant along with labels that describe them
##

#Initialize
movieNm = str("")
numTickets = int(0)
totalTicketPrice = float(0.0)

SINGLE_TICKET = int(10)

#input
name = input("Enter the name of the movie you would like to see:")
numTickets = input("Enter the number of tickets you wish to purchase:")


#process
totalTicketPrice = SINGLE_TICKET * numTickets


#output
print("Feature Film", name)
print("Cost of single ticket", SINGLE_TICKET)
print("Number of Tickets purchased", numTickets)
print("Your Total", SINGLE_TICKET * numTickets)

当您测试代码时,输​​出总是非常错误,我不知道如何修复它。谢谢!

【问题讨论】:

  • str(""), int(0) 等完全是多余的。只需 ""0 等就足以分别初始化一个字符串和一个 int。

标签: python input output


【解决方案1】:

在这一行numTickets = input("Enter the number of tickets you wish to purchase:") 中,当您获得用户输入时,您将获得string,您应该将此输入转换为int,如下所示:

numTickets = int(input("Enter the number of tickets you wish to purchase:"))

看这个例子可能对你有帮助(这个例子发生在你的代码中)

print('2'*10)
# 2222222222

【讨论】:

  • 我刚刚测试了这个,不幸的是仍然无法正常工作。关于可能导致问题的任何其他想法?
  • @vex 准确定义 what “不起作用”。你得到了什么,你期望什么?
  • @vexxedenigma 运行此代码并再次出现错误?解释你的错误是什么?
猜你喜欢
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多