【问题标题】:How to input a player name?如何输入玩家姓名?
【发布时间】:2017-06-07 05:53:27
【问题描述】:

我正在尝试创建一个角色扮演游戏,在游戏中玩家可以命名他们的角色。我遇到了一些麻烦,它说名称未定义。我了解python 2中的raw_input,这不是问题,我已经测试过,如果我将'input'更改为'raw_input'它根本不起作用,它只会结束程序。我想知道是否有人可以帮助我解决这个问题。

以下是我的代码(出于隐私目的,我已将我的名字从积分中删除):

import random

PN = 0
Hit = 0
health = 50
level = 0
EXP = 0
nameless_variable_1 = 0

print("Hello, and welcome to my creation!")
print("To begin type 1")
print("to see credits type 2")
print("to quit go to the X button in the corner and click it")
print("to continue type what you want to do and hit enter on your keyboard.")
nameless_variable_1 = input("what is your choice??????")

if nameless_variable_1 == 2:
    print("................. The entire game")
    print("do you want to play the game now?")
    print("if you do press one and then hit enter")
    nameless_variable_1 = input(" ")

if nameless_variable_1 == 1:
    PN = input("what is your character Name?")
    print("it is the year 2019 and you are trying to assasinate an important person")
    print("if you fail at this task, you can and probably will die.")

【问题讨论】:

  • 有什么问题吗??
  • 我正在就我的问题寻求帮助。
  • 你用的是python 2还是python 3?
  • 请记住input 将返回str,而不是int,因此您需要与例如'1',不是1
  • 所以我需要在 if nameless_variable_1 == 1 位中将其设为“1”而不仅仅是 1?

标签: python python-2.7


【解决方案1】:

在 python 2 中,raw_input 将返回一个字符串,而input 将返回一个评估字符串。因此input 可以很好地处理数字,因为eval('2') 给出2(作为整数)。但是它不适用于名称,因此eval("John") 会引发错误。

我建议在整个代码中使用 raw_input。 对于数字,您可以将其与字符串进行比较 if nameless_variable_1 == "2":
并且对于名称它不会抛出错误。

【讨论】:

  • 谢谢!这比我尝试做的更好!
猜你喜欢
  • 2020-01-10
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多