我是Python新手,昨天在我的PC上创建了一个基于文本的冒险游戏,这时我遇到了这个错误,我想不通。有人能给我解释一下吗?你知道吗
choice1 = input('''Oh, you. You are finally awake. You have been out cold for the last 10 hours! I am''' ,giant, ''', and I will be your guide in defeating the dark lord Thaldmemau. Well, shall we get to it?
A: Where am I?
B: Ok, we will go!
C: Who are you again?''').lower()
if choice1 == 'a':
print('You are in a recovery room in the Realm of Power, one of the seven universes of Epta. ')
elif choice1 == 'b':
print('Ok, let me just give you a brief overview of what we will do and how to fight enemies!')
elif choice1 == 'c':
print('I am' ,giant, '! I am a giant (but do not worry, I am a friendly giant). I do have some very good abilities, most of which are centred around the magic type of' ,magic, '!')
下面是一些基本上与您的代码相同的代码:
输出:
如果我改用
print
,一切都会按照您期望的方式工作:输出:
我猜这就是混乱的根源。
print
接受任意多的参数-我给它三个单独的字符串-首先是"My name is"
,然后是变量name
(也是一个字符串),最后是第三个字符串"- what's yours?"
。你知道吗input
是不同的。它完全接受0或1个参数。如果你试图给它一个大于1的值,它将产生一个TypeError
。你知道吗所以,您需要使用字符串格式来解决这个特殊的问题。其思想是生成一个字符串并将其作为参数传递给
input
函数:print
允许您提供多个str
参数,它将打印每个参数,并用空格隔开(或sep
参数指定的任何字符串)。你知道吗但是
input
需要一个单个str
参数;您自己负责连接多个字符串。例如在这一行中,您使用三个参数调用了
input
函数:如果要将字符串变量
giant
的内容放入用户看到的字符串中,可以使用字符串串联:或字符串格式:
相关问题 更多 >
编程相关推荐