Home > Blockchain >  How do I accept integers and strings all at once in python
How do I accept integers and strings all at once in python

Time:05-16

Wanted input is something like this:

Sam, 100, Josh, 200, Adam, 150 and so on...

With comma and space separating each one.

And then after accepting the input I need to calculate the average of these numbers.

I cannot use functions nor the function map(), which could be very helpful in this situation.

This is what I got so far although I think it's very far from being right. I need only one input but I was able to do it only with multiple inputs.

n = int(input())
mynames = list()
scores = list()
for i in range(n):
    name = input()
    mynames  = [name]
    for j in range(len(mynames)):
        scores  =  [int(name.split(',')[1])]
        
print(sum(scores)/n)

Thank you!

CodePudding user response:

I assume you want to have [Sam, Josh, Adam, ...] in mynames and [100, 200, 150, ...] in scores

And as it seems it's ok to use split()

n = int(input())
mynames = list()
scores = list()
input_str = input()
input_str_split = input_str.split(', ')
for i in range(n):
    mynames.append(input_str_split[i*2])
    scores.append(int(input_str_split[i*2 1]))
print(sum(scores)/n)

CodePudding user response:

Here is the solution of you Problem :

n = input()
myarray = n.split(',')
scores = []
sumOfScores = 0
studentname = []
for i in range(len(myarray)):
    if((i 1)%2 == 0):
        scores.append(int(myarray[i]))
    else: 
        studentname.append(myarray[i])
        

# Finding Sum of scores
for i in scores:
    sumOfScores  = i;

print(sumOfScores)

I hope this helps you.

CodePudding user response:

If you can't use functions then you will have to use a very lineair method:

scanning each character from the input and remembering what you have read.

input_string = 'Sam, 100, Josh, 200, Adam, 150'

mynames = []
myscores = []

name = ''
score = ''
read_characters = ''
for character in input_string:
    if character == ',':
        # new part detected
        if not name:
            # this must be a name
            name = read_characters
            read_characters = ''
        else:
            # name already read, this must be a value
            score = read_characters

            mynames.append(name)
            myscores.append(score)

            # start with a new name-value pair
            name = ''
            score = ''
            read_characters = ''
    else:
        # collect read characters untill a comma is read
        read_characters  = character

# if the input is not ending with a comma, there can be a last name-value pair
# save the last values
if name and read_characters:
    mynames.append(name)
    myscores.append(read_characters)

print(mynames)
print(myscores)

i = 0
scores_sum = 0
for score in myscores:
    scores_sum  = int(score)
    i  = 1

print(scores_sum / i)

Output

['Sam', ' Josh', ' Adam']
[' 100', ' 200', ' 150']
150.0    

If you can't even use the append function and are only interested in the average then you can also calculate the average during parsing.

input_string = 'Sam, 100, Josh, 200, Adam, 150'

scores_sum = 0
nr_of_scores = 0

name = ''
score = ''
read_characters = ''
for character in input_string:
    if character == ',':
        # new part detected
        if not name:
            # this must be a name
            name = read_characters
            read_characters = ''
        else:
            # name already read, this must be a value
            score = read_characters

            print(name, score)
            scores_sum  = int(score)
            nr_of_scores  = 1

            # start with a new name-value pair
            name = ''
            score = ''
            read_characters = ''
    else:
        read_characters  = character

# if the input is not ending with a comma, there can be a last name-value pair
# save the last values
if name and read_characters:
    print(name, read_characters)
    scores_sum  = int(read_characters)
    nr_of_scores  = 1

print(scores_sum / nr_of_scores)

The only function remaining is the conversion int() which could be not be replaced without using other functions like ord(). So I guess here stops replacing functions by mere code.

CodePudding user response:

If you know the usage of list, it can save you a lot of time and effort. For instance, let's say you are inputting all the data in one go.

print('Please insert all the required data:\n')
mydata = input()

split_result = mydata.split(',')

mynames = split_result[0::2]
scores = [float(i) for i in split_result[1::2]]

average = sum(scores)/len(scores)

print (mynames)
print (scores)
print(average)

This is what you will get instantly.

Please insert all the required data:

Sam, 100, Josh, 200, Adam, 150
['Sam', ' Josh', ' Adam']
[100.0, 200.0, 150.0]
150.0
  • Related