【发布时间】:2019-01-16 18:21:34
【问题描述】:
我有一个问题,用户可以输入空格或什么都不输入,但仍然可以通过程序,我该如何防止这种情况发生?我仍然是python的初学者。
def orderFunction(): # The function which allows the customer to choose delivery or pickup
global deliveryPickup
deliveryPickup = input("Please input delivery or pickup: d for delivery p for pickup")
if deliveryPickup == "d":
global customerName
while True:
try:
customerName = (input("Please input your name"))
if customerName == (""):
print("Please input a valid name")
else:
break
global customerAddress
while True:
try:
customerAddress = (input("Please input your name"))
if customerAddress == (""):
print("Please input a valid Address")
else:
break
global customerPhnum
while True:
try:
customerPhnum = int(input("Please input your phone number"))
except ValueError:
print("Please input a valid phone number")
else:
break
print("There will also be a $3 delivery surcharge")
elif deliveryPickup == "p":
customerName = (input("Please input your name"))
if customerName == (""):
print("Please input a valid name")
orderFunction()
else:
print("Please ensure that you have chosen d for Delivery or p for Pickup")
orderFunction()
orderFunction()
这是我的尝试,但目前我遇到了各种不缩进和缩进错误,我认为我的 while 循环可能是错误的。
基本上,如果我输入一个空格或按 Enter 键输入其中一个客户输入(例如 customerName),它就会被存储。这需要防止,我试图通过使用 while 循环来修复它,但显然没有用。
希望有人能解决这个问题
非常感谢。
【问题讨论】:
-
尝试签出string templates。它们对于获取客户信息等输入非常有用。
标签: python if-statement while-loop try-except