Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert string to int
#1
Hello,

Trying to sum string numbers to int, but there is a ; between them.

tab = input("Éntrez les nombres entiers séparés par des (;) : ")

list_nb= [tab]
list_nb = [item.replace(";"," ") for item in list_nb]

nbs = list(map(int, list_nb))

somme = 0
for nb in nbs:
    print((somme + nb)/len(nbs))
    
Error:
Éntrez les nombres entiers séparés par des (;) : 4;5;7; Traceback (most recent call last): File "<string>", line 10, in <module> ValueError: invalid literal for int() with base 10: '4 5 7 ' >
Why i get this error. Numbers need to be input with ";"

Thank you
Reply
#2
tab = input('Enter numbers seperated with (;) : ') .split(';')
print(tab)

tab = list(map(int, tab))

print(tab)

somme = 0

for number in tab:
    print((somme + number)/len(tab))
Output:
Enter numbers seperated with (;) : 5;9;6;7 ['5', '9', '6', '7'] [5, 9, 6, 7] 1.25 2.25 1.5 1.75
Frankduc likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
only thing 5+6+9+7 = 6.75 not 1.75
Reply
#4
use sum(tab) instead of len(tab)

Output:
Enter numbers seperated with (;) : 5;6;9;7 ['5', '6', '9', '7'] [5, 6, 9, 7] 0.18518518518518517 0.2222222222222222 0.3333333333333333 0.25925925925925924 6.75
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#5
user_input = input("Éntrez les nombres entiers séparés par des (;) : ")
numbers = [int(item) for item in user_input.split(";")]
print(sum(numbers) / len(numbers))
Frankduc likes this post
Reply
#6
Cant use sum in this assignment and its returning error anyway:

Error:
Éntrez les nombres entiers séparés par des (;) : 4;5;5; Traceback (most recent call last): File "<string>", line 6, in <module> File "<string>", line 6, in <listcomp> ValueError: invalid literal for int() with base 10: '' >
Reply
#7
The above error is nothing to do with sum you are trying to turn an empty string into an int
You have a ; at the end of the input string so when the split happens you end up with an empty string as the last list item.
Reply
#8
I got it now. Stupid misunderstanding

Thanks guys
Reply
#9
tab = '5;6;9;7'.split(';')
tab = list(map(int, tab))

somme = 0
total = 0

for number in tab:
    total += number
print(f'Sum of tab list is: {total}')

for number in tab:
    print((somme + number)/total)
Output:
Sum of tab list is: 27 0.18518518518518517 0.2222222222222222 0.3333333333333333 0.25925925925925924
Frankduc likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 3,355 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 3,974 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 5,281 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 4,451 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 4,781 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to path using Python 2.7 tester_V 10 9,307 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 7,510 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 9,121 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert String of an int array to a Numpy array of ints mdsousa 5 7,436 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Convert string to JSON using a for loop PG_Breizh 3 3,706 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh

Forum Jump:

User Panel Messages

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