新程序员
我试图要求用户在两个选项之间进行选择,但我无法做到正确。
inp = int(input())
while inp != 1 or inp != 2:
print("You must type 1 or 2")
inp = int(input())
if inp == 1:
print("hi")
if inp == 2:
print("ok")
quit()
即使我在最初询问时输入1或2,它仍然会回吐“你必须输入1或2”
我的最终目标是,如果他们输入1,继续该计划,而如果他们选择2,它将结束该计划。谢谢你的帮助。
答案 0 :(得分:2)
const getJobById = async () => {
try {
const response = await Axios.get(`https://api.jsonbin.io/b/5ff798661d107958ae4ca82b`)
console.log(response.data, id)
const job = response.data.filter(item => item.id === id)[0]
console.log(job)
} catch (error) {
console.log(error)
}
}
或者如果您希望他们留在这里直到他们选择 1 或 2:
inp = input("Choose 1 or 2 ")
if inp == "1":
print("You chose one")
# whatevercodeyouwant_1()
elif inp == "2":
print("You chose two")
# whatevercodeyouwant_2()
else:
print("You must choose between 1 or 2")
这可能不是最优雅的解决方案,但它与“while”循环不同。
答案 1 :(得分:1)
使用字符串。如果你需要转动" inp"变量为整数,不要围绕input()包装int()函数,包装变量本身(即int(inp))。另外,将OR更改为AND:
inp = ""
while inp != "1" and inp != "2":
inp = input("Enter 1 or 2: ")
if inp != "1" and inp != "2":
print("You must type 1 or 2")
if inp == "1":
print("hi")
if inp == "2":
print("ok")
答案 2 :(得分:0)
尝试一下
inp = ''
valid_inputs = [1,2,3,4]
output = {1: 'hi', 2:'hello', 3: 'Hey', 4: 'Bye'}
while inp not in valid_inputs:
inp = input("Enter 1 or 2 or 3 or 4: ")
if inp not in valid_inputs:
print("You must type 1 or 2 or 3 or 4")
print(output[inp])