简体   繁体   中英

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:

  • 10 is even, divide 10 by 2, it becomes 5
  • 5 is odd, decrement 5 by 1, it becomes 4 - 4 is even, divide 4 by 2, it becomes 2
  • 2 is even, divide 2 by 2, it becomes 1
  • 1 is odd, decrement 1 by 1, it becomes 0 So, 5 steps are needed to reduce 10 to 0.

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?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM