好吧,我刚刚开始学习python,并且不知道在哪里提出这个问题。我写了这个计算器并且有效。
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
print "Your answer is", a / b
if operator=="multiply":
print "Your answer is", a * b
if operator=="add":
print "Your answer is", a + b
if operator=="subtract":
print "Your answer is", a - b
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
现在,我想知道如果输入是肯定的,如何重新启动这个程序。我搜索了一个答案,但我无法弄清楚循环整个事情的命令。就像在while循环中一样,你必须在条件之后给出一个命令但是那个命令是什么? p hal。
答案 0 :(得分:3)
将其包裹在while
循环中:
repeat = "yes"
while repeat.lower().strip() == 'yes':
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
print "Your answer is", a / b
if operator=="multiply":
print "Your answer is", a * b
if operator=="add":
print "Your answer is", a + b
if operator=="subtract":
print "Your answer is", a - b
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
只要答案为“是”(不区分大小写),这将循环。最初的'重复'是为了保证循环运行至少一次。
顺便说一句,您可以将if
测试转换成字典:
import operator
ops = {'divide': operator.div,
'multiply': operator.mul,
'add': operator.add,
'subtract': operator.sub}
repeat = "yes"
while repeat.lower().strip() == 'yes':
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ").lower().strip()
if operator not in ops:
print 'Invalid operator:', operator
continue
print "Your answer is", ops[operator](a,b)
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
答案 1 :(得分:2)
while True:
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
print "Your answer is", a / b
if operator=="multiply":
print "Your answer is", a * b
if operator=="add":
print "Your answer is", a + b
if operator=="subtract":
print "Your answer is", a - b
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
if repeat == 'no':
break
虽然True会继续循环,但它可以被关键字break打破(它会突破最近的循环(for或while))。
当处理来自用户的输入以确定条件时,最好检查来自用户的输入是否以" y" (是)或以" n"开头(不),因为用户可能输入y而不是yes并且他可能会弄乱大写,这里有一个实现这个的代码:
while True:
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
print "Your answer is", a / b
if operator=="multiply":
print "Your answer is", a * b
if operator=="add":
print "Your answer is", a + b
if operator=="subtract":
print "Your answer is", a - b
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
if repeat.lower.startswith('n'):
break
"重复"是一个字符串,它有一个名为" lower"的方法(类似于函数),此方法返回字符串的小写表示,然后您可以检查该小写表示(也是一个字符串)是否以信" n"通过使用另一个名为startswith的方法。
答案 2 :(得分:0)
Try this
You can usewhile True:
with this the code written inside the for loop
will execute infinitely,
To stop execution you can use Ctrl + C
while True:
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
print "Your answer is", a / b
if operator=="multiply":
print "Your answer is", a * b
if operator=="add":
print "Your answer is", a + b
if operator=="subtract":
print "Your answer is", a - b
repeat=raw_input("Do you want to perform another calculation(yes or no)? ") if repeat == "no": break
答案 3 :(得分:0)
试试这个:
while True:
a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
print "Your answer is", a / b
if operator=="multiply":
print "Your answer is", a * b
if operator=="add":
print "Your answer is", a + b
if operator=="subtract":
print "Your answer is", a - b
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
if repeat == "no":
break
答案 4 :(得分:0)
while True:
try:
#I want this bit here to fire without waiting for the client to send anything
#right now it works except the client has to send a character first
#check for stuff to send back
for fname in os.listdir('data/%s/in' % dirname):
print(fname)
f = open('data/%s/in/%s' % (dirname, fname), "r")
client.send(f.readline())
data = client.recv(size)
if data:
bucket=bucket+data
else:
raise error('Client disconnected')
except Exception as e:
client.close()
print(e)
return False
它会一直循环直到特定条件成立