2016-04-06 9 views
-2

が、私はそれがループであれば、特定のでは動作しないの孤立ケースを持っている3.5.1Pythonは()

Pythonで.find()機能についての簡単な質問を持っているテキストゲームでエラーを返す関数、およびそのコードを.findちょうど下にある:

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() 

問題は、私は選択肢を選択したときに、それはあなたがにchoiceShopOneを割り当てる

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' 

答えて

3

を返し、お店のループで起こります関数を呼び出すのではなく、関数を呼び出します。 input().lowerの代わりにinput().lower()が必要です。あなたはchoiceShopTwochoiceShopThreeと同じミスをします。

Pythonでは、str.lowerはメソッドであり、属性ではありません。 str.lowerではなく、かっこ(str.lower())でメソッドを呼び出す必要があります。