我對編程很陌生,但是我通過創建文字游戲讓我的腳濕透了。
import random
HowManyVowels = random.randint(1,5)
vowels = ["A" , "E" , "I" , "O" , "U"]
RoundVowels = random.sample(vowels,HowManyVowels)
HowManyConsonants = 7 - HowManyVowels
Consonants = ["B" , "C" , "D" , "F" , "G" , "H" , "J" , "K" , "L" , "M" , "N" , "P" , "Q" ,"R" , "S" , "T" , "V" , "W" , "X" , "Y" , "Z"]
RoundConsonants = random.sample(Consonants,HowManyConsonants)
RoundLetters = RoundVowels RoundConsonants
print(RoundLetters)
import time
WordsGuessed = []
timeout = time.time() 60*1 # 1 minute from now
while True:
test = 0
if test == 1 or time.time() > timeout:
break
test = test - 1
PlayerWord = input("What letters can you make from these letters?")
WordsGuessed.append(PlayerWord)
print(WordsGuessed)
我已經到了可以隨機選擇一輪字母并將一輪限制為一分鐘的地方。輸入驗證是我掛斷電話的地方。因此,如果我運行代碼并選擇了字母 [ AEMRTSV ],則應該只允許用戶使用這些字母。允許重復,并且必須超過 3 長度(但這兩條規則會更容易實作)。
問題是如何將用戶輸入限制為每輪選擇的字符(大寫和小寫)。
uj5u.com熱心網友回復:
這是一個簡單的驗證方法。遍歷他們輸入的單詞中的字母,并確保它們在允許的字母串列中。它看起來像這樣:
for letter in PlayerWord: # This will loop through the input word
if letter not in RoundLetters:
print('Letter not in allowed list')
check = True
break
if check:
check = False
continue
else:
wordsGuessed.append(PlayerWord)
另外,請注意,將所有匯入陳述句組合在代碼頂部。
uj5u.com熱心網友回復:
我會給你一些部分但不是全部答案:
- 用于
in檢查字符是否包含在字串中,例如'a' in 'aeiou' == True - 如果正確選擇了字串中的每個字母,則正確選擇了字串。使用
for c in PlayerWord:回圈遍歷單詞中的每個字母并記住是否找到不正確的字母 - 用 while 回圈重復播放器輸入,直到得到正確的單詞
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444178.html
上一篇:如何從多個表中進行唯一驗證
