Python Forum
How to use while true/while loop on python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use while true/while loop on python
#1
May i ask how i can use while true / while loop to write the code.

i = "APPLE"
while True:
if i == "APPLE":
print(i)


i get Syntax Error form above code.
Reply
#2
1. From what I see, there are no indentations
2. There is a space between "print" and the parenthesis calling the function
3. I don't know if this is what you want, but it will continue to print even after i == 'apple'
Here's a better way to do this
while i != 'apple': #Will continue to loop until i is equal to "apple"
    pass #pass so there are no syntax errors
print(i) #When the while loop finishes, print will be executed
You could also use a break (Though I think the first example of code is better)
while True:
    if i == apple:
        print(i)
        break #Break out of the while loop
Reply
#3
What do you want to accomplish?

This code will endlessly print APPLE:

i = 'APPLE'
while True:
    if i == 'APPLE'
        print(i)         # Ctrl + C to cancel
Python has built-in help, type help('while') into your interactive interpreter and you will have official documentation at your disposal (press Q to quit help).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Oct-07-2019, 01:45 PM)perfringo Wrote: What do you want to accomplish?

This code will endlessly print APPLE:

i = 'APPLE'
while True:
    if i == 'APPLE'
        print(i)         # Ctrl + C to cancel
Python has built-in help, type help('while') into your interactive interpreter and you will have official documentation at your disposal (press Q to quit help).

if system output send me "APPLE" i will get APPLE else system will reply "error data"
Reply
#5
(Oct-08-2019, 01:25 AM)christing Wrote: if system output send me "APPLE" i will get APPLE else system will reply "error data"

To remove ambiguity please define 'system output', 'I will get' and 'system reply'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While True loop help Nickd12 2 1,988 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  Is using while True loop good? escape_freedom13 5 3,515 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,947 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  Returning True or False vs. True or None trevorkavanaugh 6 9,232 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 3,100 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 2,821 Feb-13-2019, 01:26 AM
Last Post: stullis
  Returning true or false in a for loop bbop1232012 3 8,127 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  Get True of false outside a loop morgandebray 2 2,449 Aug-09-2018, 12:39 PM
Last Post: morgandebray

Forum Jump:

User Panel Messages

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