在赋值python问题之前引用的局部变量

时间:2014-11-07 11:16:33

标签: python geocoding

所以我编写了一个小项目,到目前为止,我正在努力解决某个方面问题。

以下是代码:

import re

def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator")

print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = input(">>>")

print("Please enter your post code")
postCode = input(">>>")

print("Is your house a number, or a name?")
nameOrNumber = input(">>>")
if nameOrNumber == "number" or nameOrNumber == "Number":
    print("Please enter your house number")
    houseNumber = input(">>>")
elif nameOrNumber == "Name" or nameOrNumber == "name":
    print("Please enter your house name")
    houseName = input(">>>")

else:
    print("Invalid")

house = (houseNumber) + (houseName)
address = (postCode) + ", " + (house)       

print("Thank you for your information")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = input(">>>")
if clientDetailsCorrect == "no" or clientDetailsCorrect == "No":
    clientDetails()

clientDetails()

不知道出了什么问题,因为我还没有在其他任何地方引用变量。有人帮忙。

2 个答案:

答案 0 :(得分:1)

如果您发布了回溯,这将有所帮助。

那就是说,这条线可能是问题的根源:

house = (houseNumber) + (houseName)

目前编写代码的方式,只会定义houseNumberhouseName中的一个。所以Python可能会抱怨丢失的那个。

鉴于您的代码到目前为止看起来如何,它可能更好:

print("Please enter your house name or number")
house = input(">>>")

然后移除house = (houseNumber) + (houseName)行。

答案 1 :(得分:0)

试试这个:

def clientDetails():
    print("Welcome to the InHouse Solutions Room Painting Price Calculator\n")
    print("STEP 1 - CLIENT DETAILS")
    print("Please enter your full name")
    userName = raw_input(">>>")

    print("Please enter your post code")
    postCode = raw_input(">>>")

    print("Please enter your hose number or name")
    house = raw_input(">>>")

    address = "{}, {}".format(postCode, house)

    print("Thank you for your information.\n")
    print (userName)
    print (address)
    print (" ")
    print ("Is this information correct? Pleast enter Yes or No")
    clientDetailsCorrect = raw_input(">>>")
    if clientDetailsCorrect.lower().startswith('n'):
        clientDetails()

使用raw_input更好,它会将所有内容输入为字符串。此外,它还允许用户不输入引号来输入文本(我假设你将从CLI运行它)。如果你以后需要将名字中的数字分开来,那么Python有非常好的字符串方法可以用来做很多很棒的事情,我用了几个来简化代码:)

<磷>氮