Python .find()函数在文本游戏

时间:2016-04-06 21:47:03

标签: python python-3.x

我对Python 3.5.1中的.find()函数有一个简单的问题

我有一个孤立的情况,它不能在特定的if循环中工作,而且代码就在下面:

import random
import time
import sys

names = ["Bob", "Melissa", "Frank", "Mary", "John", "Denise", "Mark", "Holly"]

x = len(names)-1
randomNameGeneration = names[random.randint(0,x)]
favColors = ["blue","red","green","pink","yellow","purple","black"\
             , "gold", "white", "silver", "orange"]
y = len(favColors)-1
randomColorGeneration = favColors[random.randint(0,y)]
endingList = []

# Stats =============================================================

money = 100
health = 100
stamina = 100
sanity = 100
infection = 0
speed = random.randint(4,18)
attack = random.randint(4,18)
defense = random.randint(4,18)
aim = random.random()
charisma = random.randint(4,18)
intelligence = random.randint(4,18)
wisdom = random.randint(4,18)

inventory = []



# Functions =========================================================

def goto(linenum):
    global line
    line = linenum

# Code for gameplay =================================================
playerGender = input("What is your gender? ", ).lower()
playerSexuality = input("What is your sexuality? ", ).lower()
print ("Let's begin!")
print("----------------------------------------------")


playerName = input("What's your name? ",)

print("Well hello there " + playerName + "!")
print("It's a pleasure to meet you, I am " + randomNameGeneration+ "!")
playerFavColor = input("What's your favorite color? ", ).lower()
comFavColor = randomColorGeneration


if comFavColor == playerFavColor:
    print("Oh, "+ playerFavColor + " is my favorite color too!")
else:
    print("Cool! My favorite color is " + comFavColor)
time.sleep(2)
print("[You are currently located in a cafe next to a shopping center]")
time.sleep(1)
print("What do you want to do, " + playerName + "?")
print("[1] Go to the mall")
print("[2] Stay at the cafe")
choiceOne = input().lower()
print ("[Choice 1: " + choiceOne+ "]")

if choiceOne.find("1") != -1 or choiceOne.find("go") != -1 or choiceOne.find("mall")!= -1:
    print("Sure! Let's head off then!")
    time.sleep(3)
    print("[You arrive at the mall]")
    choiceTwo = input("Shall we go in? ", ).lower()
    if choiceTwo.find("yes") != -1:
        print("[You walk in]")
        time.sleep(2)
        print("[The mall is bustling with people]")
        print(randomNameGeneration + ": Where to?")
        print("[1]The Home Goods store")
        print("[2]The Shoe store")
        print("[3]The Clothing store")
        print("[4]The Restaurant")
        choiceThree = input().lower()
        if choiceThree.find("1") != -1 or choiceThree.find("home goods") != -1:
            print(randomNameGeneration + ": Sure!")
            exitShop = False
            while money != 0 or exitShop == False:
                print("[You have: $" + str(money)+ "]")
                print("Cashier: What do you want to buy?")
                print("[1]Knife (+5 Attack, $25)")
                print("[2]Frying Pan (+8 Attack, $40)")
                print("[3]Pepper (Ingredient for crafting, $5)")
                print("[4]Exit Shop")
                choiceShopOne = input().lower
                if choiceShopOne.find("1") != -1 or choiceShopOne.find("knife") != -1:
                    money -= 25
                    inventory += "Knife"
                elif choiceShopOne.find("2") != -1 or choiceShopOne.find("frying pan") != -1:
                    money -= 40
                    inventory += "Frying Pan"
                elif choiceShopOne.find("3") != -1 or choiceShopOne.find("pepper") != -1:
                    money -= 5
                    inventory += "Pepper"
                else:
                    exitShop = True

        elif choiceThree.find("2") != -1 or choiceThree.find("shoe") != -1:
            print(randomNameGeneration + ": Sure!")
            exitShop = False
            while money != 0 or exitShop == False:
                print("[You have: $" + str(money)+ "]")
                print("Cashier: What do you want to buy?")
                print("[1]Nikes (+10 Speed, $50)")
                print("[2]Clean Socks (+1 Defense, $5)")
                print("[3]Regular Shoes (+5 Speed, $25)")
                print("[4]Exit Shop")
                choiceShopTwo = input().lower
                if choiceShopTwo.find("1") != -1 or choiceShopTwo.find("nikes") != -1:
                    money -= 50
                    inventory += "Nikes"
                elif choiceShopTwo.find("2") != -1 or choiceShopTwo.find("clean socks") != -1:
                    money -= 5
                    inventory += "Clean Socks"
                elif choiceShopTwo.find("2") != -1 or choiceShopTwo.find("regular shoes") != -1:
                    money -= 25
                    inventory += "Regular Shoes"
                else:
                    exitShop = True

        elif choiceThree.find("3") != -1 or choiceThree.find("clothing") != -1:
            print(randomNameGeneration + ": Sure!")
            exitShop = false
            while money != 0 or exitShop == False:
                print("[You have: $" + str(money)+ "]")
                print("Cashier: What do you want to buy?")
                print("[1]Nikes (+10 Speed, $50)")
                print("[2]Clean Socks (+1 Defense, $5)")
                print("[3]Regular Shoes (+5 Speed, $25)")
                print("[4]Exit Shop")
                choiceShopThree = input().lower
                if choiceShopThree.find("1") != -1 or choiceShopThree.find("Nikes") != -1:
                    money -= 50
                    inventory += "Nikes"
                elif choiceShopThree.find("2") != -1 or choiceShopThree.find("Clean Socks") != -1:
                    money -= 5
                    inventory += "Clean Socks"
                elif choiceShopThree.find("2") != -1 or choiceShopThree.find("Clean Socks") != -1:
                    money -= 25
                    inventory += "Regular Shoes"
                else:
                    exitShop = True
        else:
            print(randomNameGeneration + ": Sure!")
            exitShop = false
            while money != 0 or exitShop == False:
                print("[You have: $" + str(money)+ "]")
                print("Cashier: What do you want to buy?")
                print("[1]Nikes (+10 Speed, $50)")
                print("[2]Clean Socks (+1 Defense, $5)")
                print("[3]Regular Shoes (+5 Speed, $25)")
                print("[4]Exit Shop")
                choiceShopFour = input().lower
                if choiceShopFour.find("1") != -1 or choiceShopFour.find("Nikes") != -1:
                    money -= 50
                    inventory += "Nikes"
                elif choiceShopFour.find("2") != -1 or choiceShopFour.find("Clean Socks") != -1:
                    money -= 5
                    inventory += "Clean Socks"
                elif choiceShopFour.find("2") != -1 or choiceShopFour.find("Clean Socks") != -1:
                    money -= 25
                    inventory += "Regular Shoes"
                else:
                    exitShop = True


    else:
        print("[You sit on a bench outside of the mall]")
        time.sleep(2)

else:
    print("That's fine with me!")
    print("Ending Got: Peaceful Cafe")

sys.exit()

问题发生在商店循环中,每当我选择一个选项时,它都会返回

Traceback (most recent call last):
  File "C:/Users/Arran/AppData/Local/Programs/Python/Python35-32/Chat Sim.py", line 94, in <module>
    if choiceShopOne.find("1") != -1 or choiceShopOne.find("knife") != -1:
AttributeError: 'builtin_function_or_method' object has no attribute 'find'

1 个答案:

答案 0 :(得分:3)

您正在为函数分配choiceShopOne,而不是调用该函数。您想要input().lower()而不是input().lower。您使用choiceShopTwochoiceShopThree犯了同样的错误。

在Python中,str.lower是一种方法,而不是一种属性。您需要使用括号调用方法:str.lower()而不是str.lower