0

Sorry if this is a newbie question I was wondering why I was getting the error "TypeError: unsupported operand type(s) for &: 'float' and 'float'" whenever I ran my program which is supposed to try to guess my number by picking the amount of numbers I tell it to (precision variable) and average the numbers in a 3 number radius to get a number close to mine. Feel free to suggest more efficient ways of writing my code I just began learning Python...

__author__ = 'Vansh'
import random


def avg(*args, length):
    nums = 0
    for num in args:
        nums += num
    return nums/length

def standby():
    i = input(">>> ")
    if i != "close":
        standby()

def ai():
    x = float(input("Enter A Number: "))
    y = int(input("Enter Precision: "))
    z = []
    for i in range(y*5):
        random_guess = random.randrange(-1, 101)
        decimal = random.randrange(-1, 10)
        random_guess = float(random_guess + (decimal/10))
        while random_guess > x-3 & random_guess < x+3:
            print()
        z.append(random_guess)
    print("Number Guess: {}".format(avg(z, len(z))))
    standby()

ai()
Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
Vansh03
  • 47
  • 9

1 Answers1

2

& is the bitwise and operator in python, I believe you want to use the and operator instead.

It should be -

while random_guess > x-3 and random_guess < x+3:

If can also be written as -

while (x-3) < random_guess < (x+3):
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176