Python 3.2.0使用def

时间:2015-01-03 06:12:23

标签: python printing function

嗨,我正在制作一个剧本,只是为了好玩,我遇到了一个小问题。

我的代码需要工作的方式,我需要回忆一个脚本,但为了做到这一点,需要在回忆起另一个脚本时召回。

我知道有点令人困惑,但它在剧本中更有意义。这是我的代码:

 # 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.

所有大字体都只是我用作笔记(#)的东西。我正在为我的朋友制作这个脚本,其名字是拉斯维加斯,所以解释了这一点。

3 个答案:

答案 0 :(得分:1)

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

可能是错的,你想要的是

name = input(...)

答案 1 :(得分:1)

两个错误:
1. 注意分配符号。
name = input("Is your name Vegas? (Y or N) >")
而不是 name == input("Is your name Vegas? (Y or N) >")

2. 变量名称错误
在方法" name()"中,更改分配给input()返回值的变量名称(" 名称"):
nameVar = input("Is your name Vegas? (Y or N) >")
如果我们不这样做,我们将得到错误:
TypeError: 'str' object is not callable
我想你已经知道为什么会这样(变量名和方法名之间的名称冲突)!

答案 2 :(得分:0)

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

问题在这里。它应该是;

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

这个,你输入的内容是错误的,所以实际输入总是 False ,这就是你的功能永远不会处理的原因。