Python:如何使用输入功能在句子的中间(填空样式)获得用户输入? [英] Python: How do I get user input in the middle of a sentence (fill-in-the-blank style) using the input function?

查看:553
本文介绍了Python:如何使用输入功能在句子的中间(填空样式)获得用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个计算营业税的程序.我首先问用户用餐的总费用是多少.然后我以以下形式问他们所在地区的营业税是多少?

I am trying to create a program that calculates sales tax. I first ask the user what was the total cost of their meal. I then ask them what is the sales tax of their area in the form of:

y = input('What is the sales tax of your area? ')

我想在问题末尾加一个百分号%",以便用户将问题视为:

I want to have a percent sign "%" at the end of the question so that the users sees the question as:

您所在地区的营业税是多少?_%,用户可以在其中输入问题和百分号之间的数字(用下划线表示).

What is the sales tax of your area?_% , where the user can enter a number between the question and the percent sign (denoted by the underline).

这是我尝试过的:

x = float(input("What is the cost of your meal? "))
y = input("What is the sales tax in your area? "+"%")

float(y)
y = y/100
cost = (x+(x*y))

print("The cost of your food is " + cost)

推荐答案

我为您提供了一个解决方案.只是一个技巧,即使搜索了很长时间也没有找到直接的解决方案.但是可以肯定的是,您需要知道输入的长度,否则对于非常长的输入,!将被覆盖.下面的代码:

I have a solution for you. Just a trick, didn't find direct solution even after searching for long. But one thing is for sure, you need to know the length of input, else for very long input the ! will be overwritten. Code below:

x = float(input("What is the cost of your meal? "))
y = input("What is the sales tax in your area?  %\rWhat is the sales tax in your area?")

y = float(y)/100
cost = (x+(x*y))

print("The cost of your food is ", cost)

这篇关于Python:如何使用输入功能在句子的中间(填空样式)获得用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆