下面是我的代码: 我创建了一个随机数列表。 变量的输入应该是 int 或 string,但我需要 如果“a”或“b”或“long”不是整数,则处理 ValueError。 事实上,程序应该处理用户为这些值写“停止”的情况, 然后程序必须停止。有简单的方法吗?
def rechercheIndiceMin(tab):
mini=tab[0]
indiceMin=0
for i in range(0,len(tab)):
if tab[i]<mini:
mini=tab[i]
indiceMin=i
return("L'indice minimum est :",indiceMin)
def rechercheIndiceMax(tab):
maxi=tab[0]
indiceMax=0
for i in range(0,len(tab)):
if tab[i]>maxi:
maxi=tab[i]
indiceMax=i
return("L'indice maximum est :",indiceMax)
import random
print("Soit un tableau tab de taille long avec des aléas [a,b]")
while True:
try:
a=int(input("Insérez a :"))
b=int(input("Insérez b :"))
long=int(input("Insérez longueur du tableau :"))
tab=[0]*long
if b<a:
buffer=a
a=b
b=buffer
for i in range(0,long):
tab[i]=random.randint(a,b)
break
except ValueError:
print("Insérer un entier svp :")
print(tab)
indiceMin=rechercheIndiceMin(tab)
print(indiceMin)
indiceMax=rechercheIndiceMax(tab)
print(indiceMax)
非常感谢,
Benoit