Search code examples
pythonindex-error

i have created the guessing game in python but it's giving an index error


from random import randint

import sys

answer = randint(int(sys.argv[1]), int(sys.argv[2]))

while True:
    try:
        guess = int(input(f'guess a number {sys.argv[1]}~{sys.argv[2]}:  '))
        if 0 < guess < 11:
            if guess == answer:
                print('you are a genius!')
                break
        else:
            print('hey bozo, I said 1~10')
    except ValueError:
        print('please enter a number')
        continue

But it's giving an index error on line 5! what to do


Solution

  • You are not passing enought parameters. You should check the lenght of argv before access to their positions:

    if len(sys.argv) != 3:
       print(f'Not enought params usage: {sys.argv[0]} min_value max_value')
       exit(-1)