用户输入是否包含字符串和整数? (蟒蛇)

时间:2019-03-01 18:36:04

标签: python

 prompt = "Enter your age for ticket price"
prompt += "\nEnter quit to exit: "

active = True

while active:   
    age = input(prompt)
    age = int(age)
    if age == 'quit':
        active = False
    elif age < 3:
        print("Your ticket is $5")
    elif age >= 3 and age < 12:
        print("Your ticket is $10")
    elif age >= 12:
        print("Your ticket is $15")         

这是一些相当简单的代码,但是我遇到一个问题。问题是,要使代码运行年龄,必须将其转换为int。但是,当您键入“退出”时,程序也应该退出。您总是可以按照“您要添加更多人吗?”的提示输入另一个提示。但是,是否有一种方法可以使其运行而不必提示另一个问题?

2 个答案:

答案 0 :(得分:1)

我建议您摆脱public class Functions { private readonly IHostService _hostService; public Functions(IHostService hostService) { _hostService = hostService; } // This function will get triggered/executed when a new message is written // on an Azure Queue called queue. public void ProcessQueueMessage([QueueTrigger("newrequests")] string dd, //DateTimeOffset expirationTime, //DateTimeOffset insertionTime, //DateTimeOffset nextVisibleTime, //string queueTrigger, //string id, //string popReceipt, //int dequeueCount, ILogger logger) { var newRequestItem = new RequestQueueItem(); logger.LogTrace($"New queue item received..."); //logger.LogInformation($" QueueRef = {id} - DequeueCount = {dequeueCount} - Message Content [Id = {newRequestItem.Id}, RequestDate = {newRequestItem.RequestDate}, Mobile = {newRequestItem.Mobile}, ProviderCode = {newRequestItem.ProviderCode}, ItemIDClass = {newRequestItem.MappingIDClass}]"); // TODO: Read the DatabaseConnectionString from App.config logger.LogTrace($" Getting DB ConnectionString..."); var connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString; // TODO: Initiation of provider service instance logger.LogTrace($" Init IalbayanmtnclientserviceClient service instance..."); var bayanService = new AlbayanMtnWCFService.IalbayanmtnclientserviceClient(); // TODO: sending request to provider service endpoint and wait for response logger.LogTrace($" Sending request to Service Endpoint..."); var response= bayanService.requestpaymenttransactionAsync("agentcode", "agentpassword", "accountno", int.Parse(newRequestItem.TransactionType), newRequestItem.MappingIDClass, newRequestItem.Mobile, (int)newRequestItem.Id).Result; logger.LogTrace($"Done processing queue item"); } } 标志,而在输入active时只需break,这样,您就可以安全地转换为"quit",因为如果输入了int,则代码不会达到该点:

"quit"

请注意,while True: age = input(prompt) if age == "quit": break age = int(age) if age < 3: print("Your ticket is $5") elif age < 12: print("Your ticket is $10") else: print("Your ticket is $15") age >= 3检查是不必要的,因为您已经通过较早的检查保证了它们。

答案 1 :(得分:0)

如果要添加另一个提示,则可以在循环之前询问第一个提示,并在循环结束时询问另一个提示。如果要添加价格,则需要一个变量。如果您不想提示其他问题,但需要更多用户输入,请将该提示留空。

prompt = "Enter your age for ticket price"
prompt += "\nEnter 'quit' to exit: "
price = 0
user_input = input(prompt)

while True:   
    if user_input == 'quit':
        break
    age = int(user_input)
    if age < 3:
        price += 5
    elif age < 12:
        price += 10
    else:
        price += 15
    print(f"Your ticket is ${price}")
    user_input = input("You can add an age to add another ticket, or enter 'quit' to exit. ")