:=怎么改成while true和break
时间: 2024-05-13 21:18:19 浏览: 134
可以这样改写:
```
while True:
user_input = input("你:")
if user_input == "退出":
print("好的,下次再见!")
break
response = chitgpt.get_response(user_input)
print("ChitGPT:", response)
```
使用 while True 创建一个无限循环,当用户输入“退出”时,使用 break 语句跳出循环。
相关问题
from goods import* goods=[] def pause(): input("回车继续:") def goodsNew (goods): gdsNo = input ("新增商品,请输入商品编号:") gdsName = input ("商品名称:") gdsPrice = input("商品单价:") gds = Goods(0, gdsNo, gdsName, gdsPrice) goods.append(gds) pause() def goodsDelete(): shan=input("请输入你要删除的商品编号:") p=0 while p<len(goods): if goods[p].gdsNo == shan: print("要删除的商品是:") del goods[p] p += 1 pause() def goodsUpdate (goods): chg = input("请输入准确的要修改的商品编号:") i = 0 while i < len (goods): if goods[i] .gdsNo == chg: print("要修改的商品是:", goods[i] .getInfo()) gdsNo = input ("修改商品,请输入商品编号:") gdsName = input ("商品名称:") gdsPrice = input("商品单价:") gdsId = goods[i].gdsId goods[i] = Goods(gdsId, gdsNo, gdsName, gdsPrice) i += 1 pause() def goodsSearch (goods): qry = input("请输入的查询内容:") for good in goods: if good .gdsNo .find(qry) >= 0 or good.gdsName.find(qry) >= 0: print("找到的商品有:", good.getInfo()) pause() def clean (): i = 0 while i < 15 : print() i += 1 menus = ["商品管理系统:-----", "1.新增商品", "2.显示已有商品", "3.查找商品", "4.修改已有商 品", \ "5.删除已有商品", "0.退出", "-------------"] def goodsMgrDo (): while True : clean() for m in menus: print (m) sel = input ("请输入一个整数,选择要完成的功能:") if sel == "0": break; elif sel == '1': goodsNew(goods) elif sel == '2': goodsSearch(goods) elif sel == '3': goodsUpdate(goods) elif sel == '4': goodsDelete() goodsMgrDo() 将前面句子中的商品改成供应商
好的,修改后的代码如下:
from supplier import* suppliers=[]
def pause():
input("回车继续:")
def supplierNew(suppliers):
supNo = input("新增供应商,请输入供应商编号:")
supName = input("供应商名称:")
supContact = input("联系方式:")
sup = Supplier(0, supNo, supName, supContact)
suppliers.append(sup)
pause()
def supplierDelete():
shan = input("请输入你要删除的供应商编号:")
p = 0
while p < len(suppliers):
if suppliers[p].supNo == shan:
print("要删除的供应商是:")
del suppliers[p]
p += 1
pause()
def supplierUpdate(suppliers):
chg = input("请输入准确的要修改的供应商编号:")
i = 0
while i < len(suppliers):
if suppliers[i].supNo == chg:
print("要修改的供应商是:", suppliers[i].getInfo())
supNo = input("修改供应商,请输入供应商编号:")
supName = input("供应商名称:")
supContact = input("联系方式:")
supId = suppliers[i].supId
suppliers[i] = Supplier(supId, supNo, supName, supContact)
i += 1
pause()
def supplierSearch(suppliers):
qry = input("请输入的查询内容:")
for sup in suppliers:
if sup.supNo.find(qry) >= 0 or sup.supName.find(qry) >= 0:
print("找到的供应商有:", sup.getInfo())
pause()
def clean():
i = 0
while i < 15:
print()
i += 1
menus = ["供应商管理系统:-----",
"1.新增供应商",
"2.显示已有供应商",
"3.查找供应商",
"4.修改已有供应商",
"5.删除已有供应商",
"0.退出",
"-------------"]
def supplierMgrDo():
while True:
clean()
for m in menus:
print(m)
sel = input("请输入一个整数,选择要完成的功能:")
if sel == "0":
break
elif sel == '1':
supplierNew(suppliers)
elif sel == '2':
supplierSearch(suppliers)
elif sel == '3':
supplierUpdate(suppliers)
elif sel == '4':
supplierDelete()
supplierMgrDo()
def guess_number(answer): while True: guess = int(input("请猜一个数字: ")) if guess == answer: print("恭喜你,猜对了!") break elif guess > answer: print("太大了,请猜小一点。") else: print("太小了,请猜大一点。") 改成预设数字,并且一直猜,直到猜对
好的,你的问题是如何将这个程序改成预设数字,并且一直猜,直到猜对。
你可以将预设数字写在程序开头,比如:
```
answer = 42 # 预设数字为42
```
然后,将程序中的 `while True` 循环改为 `while guess != answer`,这样程序就会一直猜,直到猜对为止。完整的程序如下:
```
answer = 42 # 预设数字为42
while True:
guess = int(input("请猜一个数字: "))
if guess == answer:
print("恭喜你,猜对了!")
break
elif guess > answer:
print("太大了,请猜小一点。")
else:
print("太小了,请猜大一点。")
```
注意,这里的 `guess` 变量是在每次循环内部重新赋值的,所以程序会一直猜,直到猜对为止。
阅读全文