0
votes

This is my code:

import os
from typing import Mapping

from colorama import Fore , init

init()

os.system("cls" or "clear")

clear = lambda: os.system('cls')
#GIVE A NUMBER
num = int(input(Fore.GREEN+"Please Enter A Number: "))
#GIVE A LIST NUMBER
listed = input(Fore.GREEN+"Please Enter A List Of Number: ")
# CONVERT INPUT(STR) TO LIST:
listed = [listed]
#CONVERT LIST(STR) TO LIST(INT)
listed= list(map(int, listed))
#APPEND NUMBER TO LIST:
listed.append(num)
#SORT LIST
listed = listed.sort()
#PRINT LIST
print(Fore.RED+listed)

and i got these error: Please Enter A Number: 12

Please Enter A List Of Number: 12,35,25

Traceback (most recent call last):

File "c:\Users\Administrator\Desktop\roya\1.py", line 14, in

listed= list(map(int, listed))

ValueError: invalid literal for int() with base 10: '12,35,25'

What should i do to convert all number is in input to int????

4
Side-note: os.system("cls" or "clear") only works on Windows. "cls" or "clear" always evaluates to "cls". You need to actually test the platform to determine which one to use if you want it to be portable. - ShadowRanger

4 Answers

3
votes
listed = [listed]

This creates a single-element list with the variable listed. It does not split the string into multiple ones.

To do that you need to do:

listed = listed.split(',')

Which should create the list ['12', '35', '25'] - which would be a valid input to list(map(int, listed))

0
votes

this would do

listed = list(map(int, listed.split(',')))
0
votes

There's also an error in your last line. Here's the whole thing corrected:

import os
from typing import Mapping

from colorama import Fore , init

init()

os.system("cls" or "clear")

clear = lambda: os.system('cls')
#GIVE A NUMBER
num = int(input(Fore.GREEN+"Please Enter A Number: "))
#GIVE A LIST NUMBER
listed = input(Fore.GREEN+"Please Enter A List Of Number: ")
# CONVERT INPUT(STR) TO LIST:
listed = listed.split(',')
print(listed)
#CONVERT LIST(STR) TO LIST(INT)
listed= list(map(int, listed))
#APPEND NUMBER TO LIST:
listed.append(num)
#SORT LIST
listed = listed.sort()
#PRINT LIST
print(Fore.RED+str(listed))
0
votes

If you want you can user regex expression and capture spaces when entering inputs and then convert to list from int:

import re
listed = re.split(r"\s", listed)
listed = [int(item) for item in listed]