Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

So, I recently got into programming in python and decided to make a simple code which ran some simple maths e.g calculating the missing angle in a triangle and other simple things like that. After I made the program and a few others, I thought that maybe other people I know could use this so I decided to try and make this as simple as possible. The code can be found below:

a = int(input("What's one of the angles?"))
b = int(input("What's the other angle in the triangle?"))
c = (a + b)
d = 180
f = int(180 - c)
print(f)

The code itself does work but, the only problem is that if you have more than 1 question, it becomes tedious and a rather cumbersome task to constantly load up Python and hit F5 so, my idea was to have it loop an infinite number of times until you decided to close down the program. Every time I tried searching for a way to do this, all of the while True: statements were for bigger and more complicated pieces of code and with this being maybe my fifth or tenth piece of code, I couldn't understand a few of the coding for it.

I'd appreciate any help or advice for this subject as it'd make my day if anyone's willing to help.

share|improve this question
    
That while True: suggestion is right on the money. For your first looping program you can simply add that to the top of your program, and indent everything below that. –  Mike Housky 20 hours ago
1  
c = (a + b) can simply be written as c = a + b and f = int(180 - c) can be written as f = 180 - c –  Markus Meskanen 20 hours ago
3  
And d = 180 can be written as nothing since it isn't used at all by the program :) –  Two-Bit Alchemist 20 hours ago
    
Thanks for the help, I've been able to see where I've gone wrong and how I can fix this. –  Richard Autoglass Pope 19 hours ago
    
The better solution to this (imo) is to create a shortcut that executes the program. This makes it simple to run again, requiring less extra prompts to control whether to stop the program or not. Also, when you say, "load up Python and hit F5," I'm about 99.999999% sure you're talking about an IDE. That's not Python. That's an IDE that runs Python. The Python executable can be run completely independently of the IDE. –  jpmc26 19 hours ago

3 Answers 3

up vote 6 down vote accepted

You could put the code in a function, something like:

def simple():
    a = int(input("What's one of the angles?"))
    b = int(input("What's the other angle in the triangle?"))
    c = (a + b)
    d = 180
    f = int(180 - c)
    print(f)

and then simply type:

simple()

each time to use it.

share|improve this answer
    
Thanks for the idea but, I get an error saying "expected an indented block" with the "a" variable becoming highlighted. –  Richard Autoglass Pope 20 hours ago
1  
It needs to be indented as I've shown. Indentation is very important in python (unlike other languages) so the sooner you understand it the better. –  Paul Evans 19 hours ago
    
Thanks for the help and I'm grateful that you told me about indenting something while I'm doing simple scripts in python. –  Richard Autoglass Pope 19 hours ago
while True:
    a = int(input("What's one of the angles?" + '\n'))
    b = int(input("What's the other angle in the triangle?"+ '\n'))
    c = (a + b)
    f = int(180 - c)
    print(f)
    if input("Would you like to do another? 'y' or 'n'"+ '\n').lower() == 'y':
        pass
    else:
        break

You can just ask if they want to go again. y will restart the loop, n will end it. The .lower() is in case they type Y or N.

As @Two-BitAlchemist mentioned d=180 is unecessary.

share|improve this answer
    
Thanks for the help but it's the same recurring problem as the others, I get an error saying "expected and indented block" with the "a" variable becoming highlighted. –  Richard Autoglass Pope 20 hours ago
    
Simpy make sure the indentation is correct. 4 spaces in python or a tab. What IDE are you using? They usually do it for you. –  Leb 19 hours ago
1  
@RichardAutoglassPope copy and paste problem? –  CivFan 19 hours ago
    
I've been able to fix this now and I'd like to thank everyone. –  Richard Autoglass Pope 19 hours ago
1  
Glad it worked out for you. Please accept one of the answers that helped you the most –  Leb 19 hours ago

while True is good enough for this script, why abandon it?

while True:

    a = int(input("What's one of the angles?"))
    b = int(input("What's the other angle in the triangle?"))
    c = (a + b)
    d = 180
    f = int(180 - c)
    print(f)
share|improve this answer
    
The only problem is that if I try to use the while True command, I get an error saying "expected an indented block" and the "a" variable becomes highlighted. –  Richard Autoglass Pope 20 hours ago
1  
@RichardAutoglassPope you have to get used to python indenting rules. python is clear because there are no brackets, and the price for that is indenting. –  LetzerWille 19 hours ago
    
Thanks for the help, I've managed to fix this problem now. –  Richard Autoglass Pope 19 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.