我正在尝试使用python创建一个hang子手游戏。我知道还有很多事情要做,但我想弄清楚游戏的主要组成部分,即如何将用户输入的字符串(一个字母)与三个随机选择的单词之一中的字母进行比较。>
import random
print("Welcome to hangman,guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
guess = input(str("Enter your guess:"))
guess_left = 10
guess_subtract = 1
if guess == "":
guess_left = guess_left - guess_subtract
print("you have" + guess_left + "guesses left")
答案 0 :(得分:1)
我认为您需要一个骨架,然后花一些时间来改善游戏。
import random
print "Welcome to hangman, guess the five letter word"
words = ["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
for trial in range(trials):
guess = str(raw_input("Enter character: "))
if (len(guess) > 1):
print "You are not allowed to enter more than one character at time"
continue
if guess in correct_word:
print "Well done! '" + guess + "' is in the list!"
else:
print "Sorry " + guess + " does not included..."
您的下一步可能会打印出类似c_i__的内容以及剩余的试用次数。玩得开心:)
完成实施后,请花一些时间并使用函数重新实施。
答案 1 :(得分:0)
您可以做的是将字符串视为数组/列表。 因此,如果您使用循环:
x = ""#let's assume this is the users input
for i in range(len(y)): #y is the string your checking against
if x == y[i]:
从这里开始,我想的是,如果它们相等,则会将该字母添加到字符串中
让我们说字符串是“今天很有趣”,然后会有一个像这样的数组,[“ ”。“ ”,依此类推,但是您必须找到一种将空间因素分解的方法和撇号。因此,如果字母==代表字符串的那一部分
#this is a continuation of the code, so under if
thearray[theposition] == the letter
但是,在查看代码时,您的游戏与常规代码有所不同,因此,也请考虑使用一会儿外观
gamewon = False
number of trials = 10
while gamewon == False and number of trials > 0:
#the whole checking process
if x == guess:
gamewon == True
print ........# this is if your doing word for word else use
the code above
else:
number of trials = number of trials - 1
我想就是这样,我到处都知道,但是您可以随时寻求帮助,我会解释一下,而且我指的是您认为可以解决的代码问题
答案 2 :(得分:0)
首先,您应该使用public void setMainView(View mainView) {
this.mainView = mainView;
setupView();
}
接受a
和A
。您必须在字符串中搜索猜到的字符并找到位置。
guess = guess.lower()
了解for循环的另一种方法是:
guess = guess.lower()
# Find the position(s) of the character guessed
pos = [idx for idx,ch in enumerate(correct_word) if ch == guess]
if pos:
print('Letter Found')
guess_left -= 1