2015-01-03 78 views
0

嗨我正在製作一個腳本,只是爲了好玩,我遇到了一個小問題。使用def的Python 3.2.0

我的代碼需要工作的方式,我需要回想一下腳本,但要做到這一點,它需要在召回另一個腳本時被召回。

我知道有點困惑,但它使腳本更有意義。這是我的代碼:

# Importing time and date... 
import time 
import datetime 

# Defining ans() and name(). 
def ans(): 
    print("Are you sure your name is Vegas?") 
    time.sleep(2) 
    print("Well I'm not so sure.") 
    time.sleep(1) 
    print("You'll have to prove it.") 
    time.sleep(1) 
    print("Which typewriter brand does Vegas have?") 
    print("1. Sizzix") 
    print("2. Royal") 
    print("3. Alivetti") 
    print("4. Smith-Corona") 
    ans=input(">") 
    if ans == "4": 
     print("Correct....") 
    else: 
     print("Incorrect! You are not Vegas! Liar!") 
     name() 

def name(): 
    name=0 
    print("Detecting...") 
    time.sleep(2) 
    name == input("Is your name Vegas? (Y or N) >") 
    if name == "Y": 
     ans() 
    if name == "N": 
     name() 

# Now for the actual script. This prints the time and then runs the code in name(). 
now = datetime.datetime.now() 
print(now, " --That is the time.") 
name() 
# If name == Y, then it's supposed to go to ans() and run the code there. 
# Instead it goes through the code inside name() and then stops. 

所有的大字體只是我用作筆記(#)的東西。我正在爲我的朋友製作這個腳本,名字叫拉斯維加斯,這就解釋了這一點。

+0

您的輸入是什麼?你能附上你給出的輸入和你在控制檯上看到的輸出嗎? – SMA

+0

感謝球員真的幫助:) – boatofturtles

回答

1

兩個錯誤:
1. 注意分配符號。的
name = input("Is your name Vegas? (Y or N) >")
代替
name == input("Is your name Vegas? (Y or N) >")

2. 可變
在方法的不良名 「名稱()」,改變被分配的變量( 「」)的名稱到輸入()的返回值:
nameVar = input("Is your name Vegas? (Y or N) >")
如果我們不這樣做,我們將得到錯誤:
TypeError: 'str' object is not callable
我想你已經知道爲什麼會發生這種情況(變量名和方法名之間的名稱衝突)!

1
name == input("Is your name Vegas? (Y or N) >") 

可能是錯的,你要的是

name = input(...)

0
name == input("Is your name Vegas? (Y or N) >") 

問題是在這裏。它應該是;

name = input("Is your name Vegas? (Y or N) >") 

這一點,你的輸入錯誤所以實際上你輸入始終,這就是爲什麼你的函數是永遠不會處理。