TypeError: unsupported operand type(s) for +=: NoneType and str

2024/7/6 22:10:24

I am new to Python and I'm sure that I'm doing something wrong - I would like to ask a user for three numbers and print their sum, here's my current code:

for i in range(0, 3):total = Nonenum = input('Please enter number {}:'.format(str(i)))total += num

By the way, the total = None was to try and declare the variable so I could use it without setting a value? I get this

Traceback (most recent call last):File "<pyshell#20>", line 4, in <module>total += num
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
Answer

You shouldn't do total = None. NoneType cannot be used against addition.

There's an extra problem suggested by the error message: From your description you're trying to add 3 numbers, but the return type of the builtin input() is str. So this is what you're supposed to write:

total = 0
for i in range(0, 3):num = input('Please enter number {}:'.format(str(i)))total += int(num)

All the points:

  • Indent the code correctly. Indentation is a crucial part of Python.
  • Don't set total to zero at every loop. Only set it once outside the loop
  • Take care of types
https://en.xdnf.cn/q/120241.html

Related Q&A

multiply list of ndarrays by list

I want to multiply a list of numbers, by a ndarray, that is, each number in the list multiply by the first ndarray. This is the list: list1 = [840,845,897]This is the list of ndarray list = df[Example]…

Python skipping last element in while iterating a list using for loop [duplicate]

This question already has answers here:Strange result when removing item from a list while iterating over it in Python(12 answers)Closed 9 years ago.class Sample:def __init__(self):self.lst_report_foot…

can this code be shortened or improved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Count Running and Stopped Ec2 Instances with AWS Lambda

How can I count number of running and stopped EC2 instances in a particular region using boto3 and an AWS Lambda function?

Python2.7: How to split a column into multiple column based on special strings like this?

Im a newbie for programming and python, so I would appreciate your advice!I have a dataframe like this.In info column, there are 7 different categories: activities, locations, groups, skills, sights, t…

python - return and print does not give same result

what Im trying to do is take the entered string separated by commas and change it to list then for each list as a key print the associated value.def main():myDict = {a:1, b:2, c:3, d:4, e:5....}u_input…

Creating a menuBar in a frame?

import Tkinter as tk from Tkinter import Label, StringVar, Entry, Button, Menu, Frame, Tk, Canvas import subprocess from Tkconstants import LEFT, CENTER,W, SUNKEN , X, Yclass SampleApp(tk.Tk):def __ini…

Python return dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Use selenium and python to move mouse outside of web page area

I need to test the exit intent form on this page: https://www.graphicproducts.com/guides/5s-system/When the mouse pointer is moved outside of the web page area a popup window appears. I then need to en…

Python „stack overflow” (104 if statements). Is def(x) the only solution to optimise code?

Today have tried to check files with path directory name. Previously it worked, until I tried to create 104 if/else statements. How to dispose of this overflow error? More specific question: Does def(…