School project while and for loops need some tips [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
So I have this assignment for school, so I don't want an answer just given to me if possible, but rather steered towards the right direction. Basically we've been working with while and for loops along with if, else and elif statements. We also learned about using the break command.[[enter image description here]picture of the assignment
I have put the images of the code and the assignment in here, my problem is I have the first part here but then when I move the first break statement, it just loops since it isn't under the while loop, but when i move it under the while loop, the second for loop sees the 75, and thinks it needs to be in the second loop and doesn't execute.
while True:
if choice == 1:
for a in range(200,70,-5):
print(a, end = ' ')
if a == 75:
break
while True:
if choice == 2:
for b in range(3,130,7):
print(b, end = ' ')
if b >= 129:
break

As pointed out by #Michael, it would be better if you could share your explicit code. There are code brackets that you can use for this, that enable you to have clean formatting in your message.
Anyways, I noticed that you might have some indentation issues, the 4th line that contains a print-statement seems a little far in, and shouldn't even run. Considering the validation steps that you are using, please consider that you can use the if-statements within the for or while loop. You are currently call break on the if-statement, but you can also use other callbacks like continue and pass. I think this article explains that nicely: https://www.guru99.com/python-break-continue-pass.html
Lastly, it might be worthwhile to install an IDE, like VS Code (this one is free). The IDE or Integrated Developer Environment can be helpful to organize and validate your code, making writing it so much easier.

Related

Python code that does subtracting and adding? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new to learning python, I see it's a simple language but my teacher thinks it's a good thing to give me very advanced tasks to complete, I have it due tomorrow so if anyone could manage to code this in, I will be greatly appreciated. Here is what he wants:
Detect user input for the words: "add" and "sub" aka subtraction. if the wrong answer is given, it should re-ask the question.
If the user chooses either, it should generate two numbers from 1-9, number 1 >= number 2, and the user should be asked the answer.
If the answer is correct, it should move to the next round (our teacher said there must be 5 rounds, at each end of the round the code should ask the user to type "add" or "sub" again.
if the user gives the wrong answer, it should re-ask the question.
The code ends when all 5 rounds are done, or if the user types "end" at any time with a message saying "Goodbye".
Greatly appreciated.
It is generally uncalled for people to post their homework here and have it solved, as StackOverflow is meant as a place that every question can help out a lot of people interested in your matter.
While I will not do your homework, here's what you can do:
Use "while" statements for the input check and validation, which means it will keep asking if the input is different from the value you set as required for the loop to break.
Use "If" statements for general validation checks.
What is most important though is that you take the time and read/watch tutorials that can help you get a better understanding of the language, as Stackoverflow is a place for questions of a specific nature and not a homework-solving/debugging service. Hope I could be of help!
I personally recommend the following resources for learning Python:
https://www.mikedane.com/
https://www.w3schools.com/
https://www.sololearn.com/
Personal Playlist for learning Python

Is it wrong to call main from another function in python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm new to python and I've been wondering if it is bad practice to call main from another function to essentially re-run the program if certain conditions do not apply.
Here's a code example.
def main():
userInput = input("Please input a word(\"terminate\" exits): ")
if(len(userInput)>=5):
wordLength(userInput)
else:
wordLength()
def wordLength(word = "Default print"):
if (word != "Terminate"):
print(word)
main()
if __name__ == "__main__":
main()
Is this bad practice and should I do it in a different way than this? Perhaps with a while loop?
You should use a while loop here. It is not "bad" to call main from another function, but this pattern is not widely used and would be difficult for future maintainers (even hypothetical ones) to read and understand.
It is also generally not recommended to have this type of cyclical recursive function call if at all avoidable for the same reason as well as adding even more memory to the stack.
Keep in mind that there is an edge case and exception to almost all established design rules and patterns (those listed here included), so it is a good idea to ask for second opinions when you think you might to "reinvent the wheel."

Style: Ending code blocks with `pass` vs `return`/`continue` vs nothing [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
The pass statement in python does nothing except fill space. However, I've found it useful for designating the end of a long, complicated block of code, before the program continues:
...
if condition is True:
...
pass
...
essentially, the same effect as putting the } on its own line would have, in a language like C or Java.
Similarly, I've developed the habit of replacing pass with continue at the end of loops, or return at the end of functions that wouldn't otherwise return anything, for the same reason - and to make it more clear which block of code is ending:
def someFunc():
...
while someCondition is True:
# complicated things
...
continue
...
return
When code is spaced either very narrowly or very widely, or when the code takes up a lot of vertical space, I think that putting these statements at the end of a code block makes the control flow clearer. I've done some searching, and haven't found a good source that said whether this is good or bad style (making good search keywords for this subject is annoyingly difficult). Moreover, PEP8 says nothing on the matter. I was hoping to get opinions on the subject and find what the general consensus is, if there is one.
From "Clean Code" by Robert C. Martin -
The first rule of functions is that they should be small.
The second rule of functions is that they should be smaller than that.
From The Zen of Python -
Simple is better than complex.
Readability counts.
When writing code, in any language, you are supposed to write small pieces of code (method, loop, if, etc.) that will be easy to read and understand.
By following this rule, you won't end up with long, complicated block of code as you mentioned, and as a result the need for any pass, continue or return will be gone.
Bottom line
If you got to the point where you want to add these statements - refactor your code

Python Board Game Trouble [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
this is for a school class i am learning computer scicence and i am working on a this code and i have been having some trouble and it is very hard to understand my teacher so i was wondering if anyone would be willing to help me with this program
gridl=[]
inputO=input("Please Enter").split(",") #Input Statement 1
numberO=inputO[0]#The Number Of "O" in the Input
Ocord=inputO[1:len(inputO)]#The O's
numberO=int(numberO)
for j in range(0,len(Ocord)):
gridl.append(Ocord[j-1:j+1])
for i in range(0:len(gridl))
newgrid=[]
inputX=input("Please Enter").split(",") #Input Statement 2
numberX=inputX[0] #The Number Of "X" in the Input
Xcord=inputX[1:len(inputX)]
print (Xcord)
The Rules and instructions:
Test inputs and outputs:
This is for an assignment, and you have done nothing except try (and fail) to get the first couple of lines read in. So I'm not going to be able to help you with your code - there's no code.
Instead, here are some hints:
Don't do things piecemeal if you can avoid it. When you're reading input, read all the input. When you're parsing the input, parse all the input.
You're going to make a bunch of mistakes doing this. It would help you to be able to "see" what you're doing. So, even though it's not required, I strongly suggest you make a function that will print out your game board. That way, you'll be able to "see" the situations of the pieces.
You're going to be doing some common operations, like adding pieces and getting the value of pieces. Make functions for those actions, if you can. Raise exceptions when things go wrong. The more time you spend being strict at the bottom levels, the less stupid mistakes you allow yourself to make at the top levels.
Get your types right. According to the instructions, everything in the input and output will be a number, except for the word 'None'. So you need to make sure that your input is all converted to numbers as soon as possible.

Do you have specific techniques for learning/memorizing python functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am currently learning python and have been struggling learning new functions. There always seems to be a perfect match for functions I need and I never think of them until I see them utilized elsewhere. Is this normal? Does it happen to veteran programmers as well?
for example this snippet in learnpythonthehardway:
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
I would have never thought of doing that without going in circles first.
Do you have specific mnemonic techniques for memorizing stuff? Can anyone share their insights?
Write code. Firstly, you will eventually memorise the common built-ins, like len, range, etc. There are probably a few that you will never remember at once, these are those you hardly ever use (eval, exec, compile, etc.) but that will depend on the kind of programs you are writing. That is normal when learning a programming language.
Importantly, learn how to find your way around the documentation. You might not memorise the exact argument-list for each function, but you should know the kind of features that are available, and how to look it up.
The standard library modules are another matter. You might only use 10% of them, although some people will use considerably more. Again, only learn what you need, maybe start at aspects of sys. Even so, there are obscure corners of sys that you might never need.
Don't try to remember everything, you don't need to.
Write code.
Just practice I guess.
Also, when doing something that seems "common", check the doc: https://docs.python.org/3/. Like if you want to split strings etc...
It's a pitty there is nothing like Hoogle (for Haskell) in Python (AFAIK)

Categories

Resources