大佬教程收集整理的这篇文章主要介绍了Python 猜词游戏,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作这个猜谜游戏,让用户从列表中随机猜出一个单词。我很困惑如何创建 if 语句,其中用户的输入大于或小于从水果列表中随机选择的单词的索引。
import random
fruit = ["blueBerry","grape","apple","mango","watermellon"]
fruits = random.choice(fruit)
guess = input("Guess a fruit: blueBerry,grape,apple,mango,or watermellon: \n")
while guess != fruits:
if guess < fruits.index:
print("-You've guessed low,try again")
elif guess > fruits.index:
print("-You've guessed high,try again")
elif guess == fruits:
print("You guessed the right number!")
您想将输入的 index 与答案的 index 进行比较。像这样尝试:
---------------------------------------------------------------------------
IndexError TraceBACk (most recent call last)
<ipython-input-98-10bd8239dd40> in <module>
----> 1 flood_closing = skimage.morphology.binary_closing(flood_uint8_test,footprint)
~/anaconda3/envs/eobox/lib/python3.6/site-packages/skimage/morphology/misc.py in func_out(image,SELER_335_11845@,*args,**kwargs)
37 if SELER_335_11845@ is None:
38 SELER_335_11845@ = _default_SELER_335_11845@(image.ndim)
---> 39 return func(image,SELER_335_11845@=SELER_335_11845@,**kwargs)
40
41 return func_out
~/anaconda3/envs/eobox/lib/python3.6/site-packages/skimage/morphology/binary.py in binary_closing(image,out)
142
143 """
--> 144 dilated = binary_dilation(image,SELER_335_11845@)
145 out = binary_erosion(dilated,out=out)
146 return out
~/anaconda3/envs/eobox/lib/python3.6/site-packages/skimage/morphology/misc.py in func_out(image,**kwargs)
40
41 return func_out
~/anaconda3/envs/eobox/lib/python3.6/site-packages/skimage/morphology/binary.py in binary_dilation(image,out)
75 if out is None:
76 out = np.empty(image.shape,dtype=np.bool)
---> 77 ndi.binary_dilation(image,structure=SELER_335_11845@,output=out)
78 return out
79
~/anaconda3/envs/eobox/lib/python3.6/site-packages/scipy/ndimage/morphology.py in binary_dilation(input,structure,iterations,mask,output,border_value,origin,brute_forcE)
513 for ii in range(len(origin)):
514 origin[ii] = -origin[ii]
--> 515 if not structure.shape[ii] & 1:
516 origin[ii] -= 1
517
IndexError: tuple index out of range
,
首先,您需要修正变量名称:fruits
应指代一组水果(名称),而 fruit
应指代单个水果(名称)。
fruits = ["blueberry","grape","apple","mango","watermellon"]
fruit = random.choice(fruits)
您还可以将 random.choice
替换为 random.randrange
以获取水果中随机水果的索引:
fruits = ["blueberry","watermellon"]
i_fruit = random.randrange(len(fruits))
fruit = fruits[i_fruit]
您现在可以使用循环来检查用户输入。我将使用无限循环来避免重复代码:
while True:
guess = input("Guess a fruit: blueberry,grape,apple,mango,or watermellon: \n")
# If the user guessed right
if guess == fruit:
print("You guessed the right fruit!")
# Exit the loop
break
# Find the guess index in the fruits list
i_guess = fruits.index(guess)
# Compare the indexes
if i_guess < i_fruit:
print("-You've guessed low,try again")
else:
print("-You've guessed high,try again")
,
我建议你这样做:
import random
fruits = ["blueberry","watermellon"]
fruit = random.choice(fruits)
fi = fruits.index(fruit)
def ask():
global gi
guess=input("Guess a fruit: blueberry,or watermellon: \n")
if guess not in fruits:
print("You have entered wrong word would you mind to enter right word again")
guess=ask()
else:
gi=fruits.index(guess)
return
while True:
ask()
if gi<fi:
print("-You've guessed low,try again")
elif gi>fi:
print("-You've guessed high,try again")
else :
print(f"You guessed the right answer,Yes Answer is {fruit}!")
break
这也将检查 guess
是否在 fruits
中,因为通过这样做,如果用户输入错误的单词或不在 {{1} 中的单词,您的程序将不会遇到错误此外,如果输入的单词错误,您的用户可以再次输入。
以上是大佬教程为你收集整理的Python 猜词游戏全部内容,希望文章能够帮你解决Python 猜词游戏所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。