【问题标题】:Weight python mistake SyntaxError [closed]权重python错误SyntaxError [关闭]
【发布时间】:2016-01-31 20:01:41
【问题描述】:

我正在再次审查 Python,我来到了这个我再次陷入困境的早期阶段。 我需要:

用 Python 编写一个程序来询问用户的
名称和“重量”以公斤为单位。然后程序将
输出一个句子,以克和磅为单位通知用户他们的体重。

这是我的代码:

myName = str(input('What is your name?:'))

myWeight = int(input( myName+ 'What is your weight in kilograms?:')

weightGrams = 'myWeight*1000'
weightPounds = 'myWeight*2.2'



print(myName+ 'You would weigh', weightGrams, 'in grams and' weightPounds, 'in pounds!')

我没有得到正确的输出,而且我得到了 SyntaxErrors。 语法错误在这里:

weightGrams = 'myWeight*1000'
weightPounds = 'myWeight*2.2'

最后:

print(myName+ 'You would weigh', weightGrams, 'in grams and' weightPounds, 'in pounds!')

我该如何解决这些问题?

【问题讨论】:

    标签: python input syntax-error user-input


    【解决方案1】:

    不要将int 视为string

    weightGrams = myWeight*1000
    weightPounds = myWeight*2.2
    

    您的print 也有语法错误:

    print(myName+ ' You would weigh', weightGrams, 'in grams and', weightPounds, 'in pounds!')
    

    此外,您不需要在此处转换为str,因为input 已经给出了string

    myName = input('What is your name?:')
    

    最终编辑:您在此处缺少右括号)

    myWeight = int(input( myName+ 'What is your weight in kilograms?:'))
    

    【讨论】:

    • ii 尝试过,但语法无效
    • 等等哦让我再试一次没看到顶部
    • 我也对您的语法错误进行了编辑
    • 我不断收到语法错误..?
    • 哦,我明白了,哈哈,谢谢老兄感谢它
    猜你喜欢
    • 2016-07-11
    • 2018-02-06
    • 2020-08-08
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多