Python Forum
Basic Python Program for Sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Python Program for Sequence
#1
I'm working on my homework for my programming class and I am honestly just stuck on one problem. The only thing I have so far is the prompt for user input. 

Write a program that reads a sequence of integer inputs and print
a) The smallest and largest of the inputs.
b) The number of even and odd inputs.
c) Cumulative totals. For example, if the input is 1 7 2 9, the program should print
1 8 10 19.
d) All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the
program should print 3 5 6.

I have to write a code that will allow for user input of a sequence of numbers and print them accordingly to these four guidelines.

If anyone could help me write a code and even explain it to me it would be much appreciated  :D
Reply
#2
Seems you have pretty clear instructions.
What have you tried so far?
Please post your code so we can analyze.
Clue - start by writing a list with test numbers.
Reply
#3
I doubt the prompt is right either.


def main():

#Obtain sequence on intergers from user
sequence = int(input("Enter a sequence of numbers: "))


#a) Print smallest and largers of the inputs
print(sequence)
print(min(sequence))
print(max(sequence))

#b) 



#Call main
main()
Moderator: Added Code tags - Please do so in future
Reply
#4
>>> sequence = int(input("gimme some numbers! "))
gimme some numbers! 2345 3532 3432 4,345 123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2345 3532 3432 4,345 123'
Ok, so you can't call int() on a sequence of numbers. So either int(input()) is not what you want, or you want to do it over and over and only ask for one number at a time.
Reply
#5
Let's test it and  see what happens

>>> sequence = int(input("Enter a sequence of numbers: "))
Enter a sequence of numbers: 1, 2, 5, 9, 14
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1, 2, 5, 9, 14'
invalid literal for int() with base 10: '1, 2, 5, 9, 14'

>>> sequence = int(input("Enter a sequence of numbers: "))
Enter a sequence of numbers: 1 2 5 9 14
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1 2 5 9 14'
invalid literal for int() with base 10: '1 2 5 9 14'
It's obviouse that we can't turn all numbers in integers that way. So I will remove the int() and will check what is in the sequence variabl

>>> sequence = input("Enter a sequence of numbers: ")
Enter a sequence of numbers: 1 2 5 9 14

>>> type(sequence)
<class 'str'>

>>> sequence
'1 2 5 9 14'
I see that 'sequence' holds a string. As it should be. But I can't turn all the numbers to integers at once. I have to get each of them and turn it into int type then add it to a list another variable which will contain all the numbers. That is the first step. Get the string and split it to list of "words", loop over them and for each one int(number) and append it to another list.

Here is how to do both. Splitting the input and loop over the result.
https://www.tutorialspoint.com/python/string_split.htm
https://www.tutorialspoint.com/python/py...r_loop.htm
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
sequence = input("Enter a sequence of numbers: ")
for num in sequence.split():
   mylist.append(int(num))
print(mylist)
result:

Output:
Enter a sequence of numbers: 2 3 4 5 6 [2, 3, 4, 5, 6]
Reply
#7
Steps to Follow:

Use Python input() function to accept the list as an input from a user.
use for loop to iterate over a list
Do your operation in the body of for loop

Your code should be like this
numbers = input("Enter a list element separated by space ")
list  = input_string.split()
list = map(int, list)

#first solution

print(min(list))
print(max(list))

#second problem solution

for i in list:
  if(i%2==0):
     print ("even number- ",i)
  else
     print ("odd number- ",i)

#third problem solution

sum = list[0]
for i in list:
     print ("cumulative sum is - ",sum)
     sum+=i
#4th solution try with yourself :)
Reply
#8
This thread is over a year old. Please don't necro old threads.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic program balajee 2 2,726 Sep-11-2018, 07:19 PM
Last Post: gruntfutuk
  python!TypeError: can't multiply sequence by non-int of type 'float' shaywune 2 9,502 Sep-24-2016, 04:33 PM
Last Post: shaywune

Forum Jump:

User Panel Messages

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