Reputation: 71
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????
Upvotes: 0
Views: 225
Reputation: 46
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]
Upvotes: 0
Reputation: 26
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))
Upvotes: 0
Reputation: 21275
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))
Upvotes: 3