'If a '-' contain in int input it will print something [closed]
well what im trying to do its when you input a "-Number" it will print something
and this the error im getting:
Traceback (most recent call last):
File "main.py", line 10, in <module>
if "-" in use:
TypeError: argument of type 'int' is not iterable
this is my code
n1 = 800
use = []
use2 = []
while use != n1:
use = int(input("Money: "))
use2.append(use)
print("The money that left:",800 - sum(use2))
if "-" in use:
print('ok')
Solution 1:[1]
n1 = 800
use = []
use2 = []
while use != n1:
use = input("Money: ")
use2.append(int(use))
print("The money that left:",800 - sum(use2))
if "-" in use:
print('ok')
Worked for me!
Solution 2:[2]
The line if "-" in use
attemps to see if the character "-"
is contained in the use
variable by iterating over it's characters, and that would work if the use
variable was a string, but since integers aren't iterable, an error occurs. Just check if the use
variable is below zero, since all negative integers have a - in front of them. This should work for you:
n1 = 800
use = []
use2 = []
while use != n1:
use = int(input("Money: "))
use2.append(use)
print("The money that left:",800 - sum(use2))
if use < 0:
print('ok')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Jovan Topolac |