MSS98
MSS98

Reputation: 15

input multiple entries separated by space

I'm new to python, and I have an assignment to write a program that prompts the user for a set of space-separated positive integers š‘Žš‘Ž0, š‘Žš‘Ž1, ... , š‘Žš‘Žš‘›š‘›āˆ’1. The program then reads and stores only positive integers and ignores any invalid entry

Then, your program should count the steps to reduce each number š‘Žš‘Žš‘–š‘– to 0 using these rules: ā€¢ If the number is even, divide it by 2 ā€¢ If the number is odd, decrement it by 1

For instance, to reduce the number 10:

so far in the code, I can enter one entry, but I need multiple ones separated by space.

stringInput = input("Enter integers: ")
try:
for e in stringInput:
    listOfintegers = []
    stepsCount = 0
    integerInput = int(stringInput)
    integerToTest = integerInput
    while integerToTest > 0:
        if integerToTest % 2 == 0:
            integerToTest /= 2
        else:
            integerToTest -= 1
        stepsCount += 1
    listOfintegers.append((integerInput, stepsCount))
except:
print("a string was entered")
exit(1)

print(listOfintegers)

it should be something like: Please enter a set of space-separated positive integers: 3 45 st 59 16 32 89

output:

[(3, 3), (45, 9), (59, 10), (16, 5), (32, 6), (89, 10)]

could you please help me?

Upvotes: 0

Views: 1328

Answers (2)

dimos geo
dimos geo

Reputation: 72

All you need is to use split() command by something for example split(" ") to split your input by space.

I modified your code

#stringInput = "3 45 st 59 16 32 89"
stringInput = input("Enter integers: ")
stringInput=stringInput.split(" ")
listOfintegers = []

for e in stringInput:
    stepsCount = 0
    if(e.isdigit()):
        integerInput = int(e)
    else:
        continue
    integerToTest = integerInput
    while integerToTest > 0:
        if integerToTest % 2 == 0:
            integerToTest /= 2
        else:
            integerToTest -= 1
        stepsCount += 1
    listOfintegers.append((integerInput, stepsCount))

print(listOfintegers)

Upvotes: 1

Kevin
Kevin

Reputation: 141

I think the start of your loop is a problem, as saying for e in stringInput: is really going through each character in your input string. What you probably want to, is go through each space-separated entry. There is a good function for that, split().

split() is a string function that "splits" a string into a list, where each item in the list is delimited by the argument you give. For example,

# x1 is ["1", "2", "3", "4", "5"]
x1 = "1,2,3,4,5".split(",")

# x2 is ["a", "23a", "4a5", "7"]
x2 = "a-23a-4a5-7".split("-")

So...since you want to split up your input string by spaces, you would probably write something like

stringInput = input("Enter integers: ")

# Splits up input string by spaces
inputList = stringInput.split(" ")
for e in inputList:
    listOfintegers = []
    stepsCount = 0
    integerToTest = 0
    try:
        integerInput = int(stringInput)
        integerToTest = integerInput
    except:
        print("Element is not a number")
        continue
    while integerToTest > 0:
        if integerToTest % 2 == 0:
            integerToTest /= 2
        else:
            integerToTest -= 1
        stepsCount += 1
    listOfintegers.append((integerInput, stepsCount))

print(listOfintegers)

You may need to do a little more checking to make sure that the number is positive, but this should get you started.

Upvotes: 1

Related Questions