【问题标题】:ValueError: not enough values to pack (expected 2, got 1) syntax errorValueError: no enough values to pack (expected 2, got 1) 语法错误
【发布时间】:2018-12-29 04:22:48
【问题描述】:

您好,我正在以艰难的方式学习 python 并努力理解 python 为例。我已经在书中发现了一个错误,并意识到您需要 () 来打印字符串。因此,也许语法中可能存在更多错误。当我运行这个文件时,我收到一个错误,上面写着

ValueError: no enough values to pack (expected 2, got 1) 语法错误。还有另一个一般语法错误。所以我不能将提示设置为 raw_input。

rom sys import argv

script, user_name = argv
prompt = '>'


print ("Hi %s, I'm the $s script.") % user_name, script
print ("I'd like to ask you a few questions.")
print ("Do you like %s?") % user_name  
likes = raw_input(prompt)

print ("Where do you live %s") % user_name
lives = raw_input(prompt)

print """
(Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
 And you have a %r computer. Nice)
 """& (likes, lives, computer)

【问题讨论】:

  • 发布代码或错误消息时,请复制粘贴您实际运行的文件中的代码,并复制粘贴实际运行的错误消息错误输出。看起来您的代码和错误消息都歪曲了。

标签: python-2.x


【解决方案1】:

代码示例中有错字吗?格式字符串中的$s而不是%s

我怀疑你的实际代码有这个:

print ("Hi %s, I'm the %s script.") % user_name, script

你的意思是写这个:

print "Hi %s, I'm the %s script." % (user_name, script)

也就是说,打印一个字符串,它是将格式字符串应用于两个参数的结果。

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2017-12-23
    • 2020-11-19
    • 1970-01-01
    • 2020-10-26
    • 2022-07-22
    • 2022-11-12
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多