Python 3.2.0使用d

2024-04-25 22:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我现在正在做一个剧本,只是为了好玩,我遇到了一个小问题。你知道吗

我的代码需要以这种方式工作,我需要调用一个脚本,但要做到这一点,它需要在调用另一个脚本时被调用。你知道吗

我知道有点困惑,但在剧本里有点道理。这是我的密码:

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

所有的大字体都是我用来做笔记的。我是为我的朋友维加斯写剧本的,这就解释了。你知道吗


Tags: andthetonamedatetimeiftimecode
3条回答
name == input("Is your name Vegas? (Y or N) >")

可能是错的,你想要的是

name = input(...)

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

问题就在这里。它应该是

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

因此,您的输入是错误的,所以实际上您的输入总是,这就是为什么您的函数从不处理。你知道吗

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

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

相关问题 更多 >

    热门问题