Last answered:

10 Dec 2020

Posted on:

27 Jun 2020

0

Working with loops in Python and handling string and int format

Hello Team,
I am using following code for calculating mean of sequence numbers.
user_input = input('Plese enter enteger and type exit to stop:> ')
numbers = []
while user_input != 'exit':
----while user_input.isalpha():
--------print('Please enter enteger only')
--------user_input = input('Please enter enteger again:> ')
----user_input = int(user_input)
----numbers.append(user_input)
----print(f'{user_input} has been added successfully.')
----user_input = input('Please enter next enteger and type exit to stop:> ')
total = 0
for i in numbers:

----total += i
print(f'Mean of numbers is {total/len(numbers)}')
I have two questions regarding above code:

  1. When i enter string value it gives me message "Please enter enteger only" . It is OK. Then, i enter 'int' numbers three or four and then i type 'exit' it calculate me 'mean' successfully but when i type two or three 'int' numbers and then i type 'str' like cat, then it gives me error and does not display message like 'Please enter enteger only'.
  2. How to deals with both 'str' and 'int' while using input function. please try above code and suggest me some solution.

 

1 answers ( 0 marked as helpful)
Instructor
Posted on:

10 Dec 2020

0
Hi Adnan,  thank you for reaching out and appologies for the late response. Could you please share with us which course and lecture your question is referring to? Thank you in advance!  Best,  365 Eli

Submit an answer