gpt4 book ai didi

python - python为什么显示 'list index out of range'错误?

转载 作者:行者123 更新时间:2023-12-03 08:47:00 24 4
gpt4 key购买 nike

我是python的新手,仅在1周前开始学习。该程序非常有效,除了当我在以0开头的guess1变量中输入数字时。

import random
import sys
def script():
while True:
number1 = random.randint(1000, 9999)
number1 = int(number1)
while True:
print ("Enter Your Guess")
guess1 = input()
guess1 = int(guess1)
while True:
if guess1 != number1:
break
elif guess1 == number1:
print ("Your Guess Was Right!")
print ("Do you want to play again? Type YES or NO")
ask = input()
ask = str(ask)
if ask == "YES" or ask == "yes":
script()
elif ask == "NO" or ask == "no":
sys.exit()
else:
print ("Invalid input, try again.")
continue
number = list(str(number1))
guess = list(str(guess1))
if len(guess) > 4:
print ("Please type a 4-digit number")
continue
bulls = 0
wr = 0
cows = 0
a = 3
while a >= 0:
if number[a] == guess[a]:
number[a] = 'a'
guess[a] = 'b'
bulls += 1
a -= 1
b = 0
c = 0
while b < 4:
c = 0
while c < 4:
if number[b] == guess[c]:
number[b] = 'a'
guess[c] = 'b'
wr += 1
c += 1
b += 1
z = bulls + wr
cows = 4 - z
bulls = str(bulls)
cows = str(cows)
wr = str(wr)
print ("Cows: "+cows)
print ("Bulls: "+bulls)
print ("Wrongly Placed: "+wr)
break
script()

这是一个为游戏编写的程序,其中要猜测一个4位数字。我们从一个随机数开始,然后以奶牛,公牛和放错位置的形式获得线索。牛表示数字错误,公牛表示数字正确,放错位置表示数字正确但放错位置。
整个过程正常工作,但是当我输入一个以0开头的数字时,它显示如下内容:-
Traceback (most recent call last):
File "GuessingGame.py", line 61, in <module>
script()
File "GuessingGame.py", line 36, in script
if number[a] == guess[a]:
IndexError: list index out of range

请帮忙,谢谢!

更新:

感谢用户@blue_note的回答,该程序现在可以运行!这就是它的修改方式-
import random
import sys
def script():
while True:
number1 = random.randint(1000, 9999)
number1 = int(number1)
while True:
print ("Enter Your Guess")
guess1 = input()
number = list(str(number1))
guess = list(str(guess1))
if guess[0] == 0:
guess1 = str(guess1)
else:
guess1 = int(guess1)
while True:
if guess1 != number1:
break
elif guess1 == number1:
print ("Your Guess Was Right!")
print ("Do you want to play again? Type YES or NO")
ask = input()
ask = str(ask)
if ask == "YES" or ask == "yes":
script()
elif ask == "NO" or ask == "no":
sys.exit()
else:
print ("Invalid input, try again.")
continue
bulls = 0
wr = 0
cows = 0
a = 3
while a >= 0:
if number[a] == guess[a]:
number[a] = 'a'
guess[a] = 'b'
bulls += 1
a -= 1
b = 0
c = 0
while b < 4:
c = 0
while c < 4:
if number[b] == guess[c]:
number[b] = 'a'
guess[c] = 'b'
wr += 1
c += 1
b += 1
z = bulls + wr
cows = 4 - z
bulls = str(bulls)
cows = str(cows)
wr = str(wr)
print ("Cows: "+cows)
print ("Bulls: "+bulls)
print ("Wrongly Placed: "+wr)
break
script()

由于我的猜测总是错误的,如果第一个数字为0,则无需将其转换为int。

再次感谢您的帮助!这是我在网站上的第一个问题。这确实是一个很酷的网站。

最佳答案

当您传递以0开头的整数(例如0123),并在下一行将其转换为int时,剩下的是123(3位数字)。稍后,您执行number = list(str(number1)),因此您的number['1', '2', '3'](长度3)。然后,尝试使用number[a]获取a=3,那就是您得到了错误。

你可以做类似的事情

number = list(str(number1) if number1 > 999 else '0' + str(number1))

关于python - python为什么显示 'list index out of range'错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497682/

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