The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Working with loops in Python and handling string and int format

Working with loops in Python and handling string and int format

0
Votes
0
Answer

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.

 

No answers so far.
×
Complete Data Science Training
Save 60%