[Tutor] Fwd: Re: Adding consecutive numbers
Dave Angel
davea at davea.name
Wed May 6 16:01:22 CEST 2015
On 05/06/2015 07:51 AM, Alan Gauld wrote:
> Please use ReplyAll to include the list members.
>
>
> -------- Forwarded Message --------
> Subject: Re: [Tutor] Adding consecutive numbers
> Date: Wed, 6 May 2015 21:13:15 +1000
> From: Whom Isac <wombingsac at gmail.com>
> To: Alan Gauld <alan.gauld at btinternet.com>
>
>
>
> Thanks for the reply. I am sorry that I did not notice the mail. I am
> actually using the latest version of python (3.5) in windows 7 operating
> system. I have already made certain changes in the code. I understood my
> mistake. The correction's are not finished yet,though. You can have a
> look at it, because, I donot know what I have written is already in
> right syntax or not.
>
> Here are my code:
> ##Goal: Building a math program.
> ## two nums will be asked by the user
> ## they will be added
> ## condition: num >=o:
> ## num will continue to be added into a list untill the second number
> ## For your information, a consequitive sequence of num : num-->1
> num1--> num+1...+n
>
> if __name__=='__main__':
> interact()
You get an error right there, since interact() isn't defined yet. Move
the above two lines to the end of the file.
>
> def interact():
> print('''Welcome to My new Math program!!
> With this program, you can find the sum of any consequitive
> numbers.''')
> print('So Just add your numbers in following spaces')
> ## If anybody complaining about this function. I will have to say,
> that the coding is incomplete so
> ## I will first define all my function then def interact() when I
> am finishing.
>
>
> def getting_numbers(first_num, second_num):
> x = [] #This is a empty list to store data
> y = [] #This is a empty list to store data
> """Getting the user values:"""
> first_num =int(input('Please enter your first number: '))
> x.append(first_num) # adding the input in x#
> second_num =int(input('Please enter your second number: '))
> y.append(second_num) # adding the input in x#
> z =(x,y) # This is a touple containing both x and y value.
> return z
Why are you so enamored with lists? You're returning a tuple containing
two lists each of which has exactly one value? Why not just return a
tuple of first_num and second_num ?
>
> def adding_all(x):
> total = 0
> for num in x:
> total +=num
> return total
Good function.
> def remove_letter(x):
> if x != len(x):
What do you think that statement does? It can't possibly do anything
useful since the right side assumes that x is a collection or
equivalent, and the left side assumes that x is a number.
> print('You did not enter a number')
> elif x != adding_all(x):
> print("Please, donot include letters")
> else:
> return x
> ## I think using a while True function to iterate all item in x
> would be better.
>
Considering that after you call each input() function, you immediately
call int(), I'd figure that checking for "you did not enter a number" is
superfluous.
>
>
> def adding_number(x,y):
> start = x[0]
> end = y[0]
> new_x = 0
> new_x_1 = 0
> while x[0]<=y[0] or x[0]<= 0:
> if x[0]==0:
> new_x+=1
> return new_x
> elif x[0]>0 or x[0]<y[0]:
> new_x_1+=x[0]
> return new_x_1
> else:
> pass
> print("You have not input a digit in order, check your
> digits\n")
> print("I donot know what you mean?")
>
I can't make any sense out of anything in this function.
I think you need to write one function and include descriptive comments
in it, and write code that tests it against those comments. Then when
you have one function that successfully runs, write a second one.
--
DaveA
More information about the Tutor
mailing list