Python Forum
weird error on my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
weird error on my code
#1
I made this code to add random numbers to an array and print the sums, all of that works except the code engine im using requires me to add "Total is:" to the sum of the random numbers, but i get an error when trying to do this, i did this o many other occasions and this doesnt work for some reason.

the error is :
Error:
Traceback (most recent call last): File "question2.py", line 13, in <module> test() File "question2.py", line 9, in test print ("Total:" + stuff) TypeError: must be str, not int
def test():
    import random
    stuff = 0
    num = int(input("How many values to add to the array: "))
    for x in range(num):
        array.append(random.randint(10,99))
    stuff = sum(array)
    print (array)
    print ("Total:" + stuff)
Reply
#2
which version of python are you using?
if 3.6 or newer:
import random

def test():
    stuff = 0
    array = []
    num = int(input("How many values to add to the array: "))
    for x in range(num):
        array.append(random.randint(10,99))
    print (array)    
    stuff = sum(array)
    # If using python 3.6 or newer 
    print (f"Total: {stuff}")
    # otherwise use:
    # print (f"Total: {stuff}.format()"

test()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020