gpt4 book ai didi

python - 如果条件在 python 中的循环内

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:57 25 4
gpt4 key购买 nike

我是 python 的新手。我确信这是一个非常基本的问题,但我仍然没有在 python 中得到它。

我有两个一维数组,长度为 50 的 A 和 B。我想为给定的用户输入找到 A[0],我必须返回 B[0]、A[1]——> B[1] 等等。

我已经为此任务创建了一个函数。

 A = [10, 20,.... 500]
B = [1, 4,.... 2500]

def func():
x = input("enter a value from the array A: ") #user input
for i in range(50):
if A[i] == x:
print(B[i])

else:
print("do nothing")

func()

但是如果我调用函数,我什么也得不到。如果有人能帮助我,我将不胜感激。谢谢。

最佳答案

试试这个

  A = [10, 20,.... 500]
B = [1, 4,.... 2500]

def func():
x = int(input("enter a value from the array A: ")) #user input
for i in range(50):
if A[i] == x:
print(B[i])

else:
print("do nothing")

func()

关于python - 如果条件在 python 中的循环内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com