Python Forum
instruction refused on a "while loop"
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
instruction refused on a "while loop"
#1
my question here:Why the following tiny  file of 4 lines works well? 1. value =5.5...    2.total=0.0....3. total=value+total....4...print(total)....This works well....but the following code does not work

# file5.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press ENTER to indicate the end\n")
total = 0.0
count = 1
while (count != 0):
   value = input("enter the value: ")
   print(value)
   print(total)
   total=value+total#not accepted. <interpreter speaks of "module"
   print(total)
   # if value.strip == '':#stop inputs on CR
   average = total / count
   print("average value= ", average)
   count = count + 1
Reply
#2
'not working' is not very informative. Please, in the future post full Traceback in error tags. Usually the traceback is pretty informative what the error is where it is.
In this particular case the problem is that input return string (i.e. type str). So you need to convert the user input to numbers using built-in functions float(), int() before you can use in calculations.
Reply
#3
In addition to @buran's post, I will say that this while loop will never stop. The statement always will be True.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
HERE is more information given by the output........................................................................................................................enter the value: 4.8
4.8
0.0

Error:
Traceback (most recent call last):  File "/home/sylvain/PycharmProjects/py2/file5.py", line 12, in <module>    total=value+total#not accepted. <interpreter speaks of "module" TypeError: Can't convert 'float' object to str implicitly
Process finished with exit code 1
Reply
#5
(May-19-2017, 05:36 PM)buran Wrote: In this particular case the problem is that input return string (i.e. type str). So you need to convert the user input to numbers using built-in functions float(), int() before you can use in calculations.
Reply
#6
My first error disappeared.  I did correct it.The problem now is that it is impossible to stop inputs on a CR. Have you an idea of a character to use in order to stop inputs. Here is my new code : ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# file5.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press ENTER to indicate the end\n")
total = 0.0
value = 0.0
count = 1
while (count != 0):
    value = float(input("enter the value: "))
    print(value)
    print(total)
    total = value + total  
    print(total)
    if value == '':  # stop inputs on CR or other character
        average = total / count
        print("average value= ", average)
        count = count + 1
Reply
#7
(May-19-2017, 06:15 PM)wavic Wrote: In addition to @buran's post, I will say that this while loop will never stop. The statement always will be True.
well, it's up to you - when do you want it to stop? i.e. until user enter something specific, when he count reach certain value, etc...
Reply
#8
I wish to stop inputs on a specific character or combination of 2 characters ( use of &&) for example. I am a beginner in python so I am unable to finish that. Thanks for your cooperation.
Reply
#9
Cool, show us what you've got so far :)
Reply
#10
(May-19-2017, 08:18 PM)sylas Wrote: My first error disappeared.  I did correct it.The problem now is that it is impossible to stop inputs on a CR. Have you an idea of a character to use in order to stop inputs. Here is my new code : ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# file5.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press ENTER to indicate the end\n")
total = 0.0
value = 0.0
count = 1
while (count != 0):
    value = float(input("enter the value: "))
    print(value)
    print(total)
    total = value + total  
    print(total)
    if value == '':  # stop inputs on CR or other character
        average = total / count
        print("average value= ", average)
        count = count + 1

Your immediate conversion to float will cause exception if you just press CR when prompted
>>> value = float(input())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
>>>
You will leave the loop - without getting the results
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Illegal instruction (core dumped) when import pandas. amandacstr 1 2,088 Dec-31-2022, 11:31 PM
Last Post: stevendaprano
  Illegal instruction? working code for months? korenron 4 13,056 Aug-05-2021, 09:57 AM
Last Post: korenron
  Docker-compose on Pycharm - Connection refused rstambach 3 3,386 Apr-08-2021, 03:07 AM
Last Post: rstambach
  permutations calculation with a while instruction kwak86 2 2,266 Sep-10-2018, 02:09 PM
Last Post: kwak86
  Nginx Uwsgi Django 111 Connection refused while connecting to upstream sandyman 0 5,103 Sep-18-2017, 07:05 AM
Last Post: sandyman

Forum Jump:

User Panel Messages

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