我正在制作一個口袋妖怪猜謎游戲,其中給出了高度和型別,除了一件事,高度檢查器外,一切都很好。
高度檢查器應該告訴您猜出的口袋妖怪是比選擇的口袋妖怪高還是小。無論用戶輸入如何,輸出始終為the pokemon is a little taller than what you chose. 代碼如下:
import random
life = 4
genone = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander ", "Charmeleon", "Charizard",
"Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree",
"Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata",
"Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu",
"Sandshrew", "Sandslash", "Nidoran♀", "Nidorina", "Nidoqueen", "Nidoran♂",
"Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales"
"random poke"]
genonetypes = ["grass", "grass", "grass", "fire ", "fire", "fire/dragon", "water",
"water", "water", "bug", "bug", "bug", "bug", "bug", "bug", "flying",
"flying", "flying", "normal", "normal", "flying", "flying", "poison",
"poison", "electric", "electric", "ground", "ground", "poison", "poison",
"poison", "poison", "poison", "poison", "fairy", "fairy", "fire", "fire",
"tester"]
genoneheight = ["0.7m", "1m", "2m", "0.6m", "1.1m", "1.7m", "0.5m", "1m", "1.6m", "0.3m",
"0.7m", "1.1m", "0.3m", "0.6m", "1m", "0.3m", "1.1m", "1.5m", "0.3m",
"0.3m", "0.7m","0.3m","1.2m","2m","3.5m","0.4m","0.8m","0.6m","1m","0.4m",
"0.8m", "1.3m","0.5m","0.9m","1.4m","0.6","1.3","0.6m","1.1","4m"]
x, y, z = random.choice(list(zip(genone,genonetypes,genoneheight)))
while True:
print(x)
print(z)
print("The pokemon type is " y " and they are " z " tall (meters)")
userin = input("enter the pokemon you think it is: ")
if userin.lower() == x.lower():
print("yes!!! great job you got it!!!11!!!!")
break
if userin != x:
life = life-1
print("that is incorrect you have " str(life) " lives left\n")
print()
# height check
if userin > z:
print("the pokemon is a little taller than what you chose")
if userin < z:
print("the pokemon is a little smaller than what you chose")
if life == 0:
print("the pokemon was " x " .")
print("better luck next time")
break
if宣告或放置位置是否有問題?
uj5u.com熱心網友回復:
您可能想要跟蹤變數userin和z. 從您的程式中,userin應該是“genone”(例如 Bulbasaur),z而是一串高度(例如“0.7m”)。因此,對于諸如 的條件陳述句if userin < z,您正在比較兩個不同的事物(例如“genone”<“0.7m”)。至于字串比較,'0' 小于 'g' 所以你總是the pokemon is a little taller than what you chose列印出來的。
你要做的是,首先,找到“genone”的“genoneheight”,然后將這個高度與所選口袋妖怪的“genoneheight”進行比較。
一個更方便的方法是定義一個字典,其中 genone 作為鍵,genonetypes 和 genoneheight 作為值。
uj5u.com熱心網友回復:
userin是我想的口袋妖怪的名字。userin總是會比z在這種情況下更大。
你必須做的是:
if genoneheight[genone.index(userin)] > z:
print('Taller')
else:
print('Shorter')
userin這會從 genoneheight 獲取 genone的索引位置索引中的元素。這聽起來可能令人困惑。如果有效請回復。另外,我建議您使用float而不是str高度。
uj5u.com熱心網友回復:
為了比較高度,您需要隨機選擇的高度和口袋妖怪猜測用戶輸入的高度。為了使后者更容易,我創建了一個查找表lut字典,將口袋妖怪名稱映射到它們的型別和高度. 創建的表如下所示:
{'bulbasaur': ('grass', '0.7m'),
'ivysaur': ('grass', '1m'),
'venusaur': ('grass', '2m'),
'charmander ': ('fire ', '0.6m'),
'charmeleon': ('fire', '1.1m'),
'charizard': ('fire/dragon', '1.7m'),
'squirtle': ('water', '0.5m'),
'wartortle': ('water', '1m'),
'blastoise': ('water', '1.6m'),
'caterpie': ('bug', '0.3m'),
'metapod': ('bug', '0.7m'),
'butterfree': ('bug', '1.1m'),
'weedle': ('bug', '0.3m'),
'kakuna': ('bug', '0.6m'),
'beedrill': ('bug', '1m'),
'pidgey': ('flying', '0.3m'),
'pidgeotto': ('flying', '1.1m'),
'pidgeot': ('flying', '1.5m'),
'rattata': ('normal', '0.3m'),
'raticate': ('normal', '0.3m'),
'spearow': ('flying', '0.7m'),
'fearow': ('flying', '0.3m'),
'ekans': ('poison', '1.2m'),
'arbok': ('poison', '2m'),
'pikachu': ('electric', '3.5m'),
'raichu': ('electric', '0.4m'),
'sandshrew': ('ground', '0.8m'),
'sandslash': ('ground', '0.6m'),
'nidoran?': ('poison', '1m'),
'nidorina': ('poison', '0.4m'),
'nidoqueen': ('poison', '0.8m'),
'nidoran?': ('poison', '1.3m'),
'nidorino': ('poison', '0.5m'),
'nidoking': ('poison', '0.9m'),
'clefairy': ('fairy', '1.4m'),
'clefable': ('fairy', '0.6m'),
'vulpix': ('fire', '1.3m'),
'ninetalesrandom poke': ('fire', '0.6m')}
使用此表和名為to_float()將高度字串轉換為浮點數以進行正確數值比較的輔助函式,我想出了以下內容:
import random
life = 4
genome = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander ", "Charmeleon", "Charizard",
"Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree",
"Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata",
"Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu",
"Sandshrew", "Sandslash", "Nidoran♀", "Nidorina", "Nidoqueen", "Nidoran♂",
"Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales"
"random poke"]
genometypes = ["grass", "grass", "grass", "fire ", "fire", "fire/dragon", "water",
"water", "water", "bug", "bug", "bug", "bug", "bug", "bug", "flying",
"flying", "flying", "normal", "normal", "flying", "flying", "poison",
"poison", "electric", "electric", "ground", "ground", "poison", "poison",
"poison", "poison", "poison", "poison", "fairy", "fairy", "fire", "fire",
"tester"]
genomeheight = ["0.7m", "1m", "2m", "0.6m", "1.1m", "1.7m", "0.5m", "1m", "1.6m", "0.3m",
"0.7m", "1.1m", "0.3m", "0.6m", "1m", "0.3m", "1.1m", "1.5m", "0.3m",
"0.3m", "0.7m","0.3m","1.2m","2m","3.5m","0.4m","0.8m","0.6m","1m","0.4m",
"0.8m", "1.3m","0.5m","0.9m","1.4m","0.6m","1.3m","0.6m","1.1m","4m"]
# Create lookup table.
lut = {name.lower(): (type, height)
for (name, (type, height)) in zip(genome, zip(genometypes, genomeheight))}
# Utility function.
def to_float(text):
"""Return text string converted to float."""
# Ignore last character unless it a digit.
end = -1 if not text[-1].isdigit() else len(text)
return float(text[:end])
# Randomly select genome and get associated information.
x, y, z = random.choice(list(zip(genome, genometypes, genomeheight)))
x, (y, z) = 'ivysaur', lut['ivysaur']
while True:
print(x)
print(z)
print("The pokemon type is " y " and they are " z " tall (meters)")
userin = input("Enter the pokemon you think it is: ").lower()
if userin not in lut:
print("Sorry, that's a unrecognized pokemon. Please try again.")
continue
if userin == x:
print("Yes!!! Great job you got it!!!!!!!")
break
else:
life -= 1
print("That is incorrect you have " str(life) " lives left\n")
print()
if life == 0:
print("The pokemon was " x " .")
print("Better luck next time")
break
# Height check.
userin_height = to_float(lut[userin][1]) # Get height of associated pokeman.
float_z = to_float(z) # Convert to float.
if userin_height > float_z:
print("The pokemon is a little smaller than what you chose")
elif userin_height < float_z:
print("The pokemon is a little taller than what you chose")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/463713.html
