Posts: 6
Threads: 1
Joined: Jul 2017
Hello guys,
Firstly i really appreciate that you spend your time helping me instead of doing some else.
I am a beginner in python, and i coded a program like this :
I want to make a code that when word is typed wrong ask for it again and when is typed correctly move to the next one. But i am facing problems all the time.(i want to print a word (success) if the word is correct and print the word(fail) if the word is wrong).
Thanks for your time
indignant= input("Δώσε μου την σημασία της λέξης indignant\n");
while (indignant!= "αγανακτησμένος"):
indignant= input("Δώσε μου την σημασία της λέξης indignant\n");
if (indignant== "αγανακτησμένος"):
print("Σωστό");
else: print("Λάθος");
tiger = input("Δώσε μου την σημασία της λέξης tiger\n");
while (tiger != "τίγρη"):
if(tiger == "τίγρη"):
print("Σωστό");
else: print("Λάθος");
wall = input("Δώσε μου την σημασία της λέξης wall\n");
while (wall != "τοίχος"):
if(wall == "τοίχος"):
print("Σωστό");
else: print("Λάθος");
leg = input("Δώσε μου την σημασία της λέξης leg\n");
while (leg != "πόδι"):
if(leg == "πόδι"):
print("Σωστό");
else: print("Λάθος");
break;
Posts: 1,298
Threads: 38
Joined: Sep 2016
well first, line 7 is indented and probably shouldn't be.
line 22 should be indented 1 more level to match line 20
lines 6, 11, 16 and 21 the print() functions should be on their own lines, just like the 'if' statements
You have way to many unnecessary parenthesis. In your case, they are only required for the 'input()' and 'print()'. None of the semi-colons (';') are needed.
Since your code is in English, it would be helpful if the prompts were as well.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 1
Joined: Jul 2017
Yes but how do I make it to ask for the word again if the word isnt correct?
Posts: 4,220
Threads: 97
Joined: Sep 2016
Well, you have to code it to ask again. You need to ask in the loop. You are only doing that for indignant, not for the other words. Also, you need breaks when you get the correct answer to get out of the loop. Otherwise one wrong answer and you're stuck forever.
Posts: 6
Threads: 1
Joined: Jul 2017
when i put the break tag's i face a error that leaves only when i remove the break tag
Posts: 1,298
Threads: 38
Joined: Sep 2016
(Jul-07-2017, 11:09 AM)Basilis Wrote: when i put the break tag's i face a error
Where in your code did you put the 'break' ? (post the relevant code between the code tags), What is the full error that you receive? (post the complete error code between the error tags)
Did you clean up your code as I suggested, to make it easier for you and us to read and follow?
Here is an example of your first loop, cleaned up:
indignant = input("Δώσε μου την σημασία της λέξης indignant\n")
while indignant != "αγανακτησμένος":
indignant = input("Δώσε μου την σημασία της λέξης indignant\n")
if indignant == "αγανακτησμένος":
print("Σωστό")
break
else:
print("Λάθος")
continue
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 1
Joined: Jul 2017
firstly i put lot of parenthesis and semi-colons bc its more familiar to me due to javascript!
my new code is this :
name = input("Πληκτρολόγησε το όνομα σου \n");
print("Καλησπέρα ",name, ", \nΣήμερα θα εξετάσουμε τις δυνατότητες σου στις λέξεις");
indignant = input("Δώσε μου την σημασία της λέξης indignant\n");
while (indignant != "αγανακτισμένος"):
indignant = input("Δώσε μου την σημασία της λέξης indignant\n");
if (indignant == "αγανακτισμένος"):
print("Σωστό");
score = score + 1
break;
else: print("Λάθος");
indignant = input("Δώσε μου την σημασία της λέξης indignant\n");
tiger = input("Δώσε μου την σημασία της λέξης tiger\n");
while (tiger != "τίγρη"):
if(tiger == "τίγρη"):
print("Σωστό");
score = score + 1
break;
else: print("Λάθος")
tiger = input("Δώσε μου την σημασία της λέξης tiger\n");
wall = input("Δώσε μου την σημασία της λέξης wall\n");
while (wall != "τοίχος"):
wall = input("Δώσε μου την σημασία της λέξης wall\n");
if(wall == "τοίχος"):
print("Σωστό");
score = score + 1
break;
else: print("Λάθος");
wall = input("Δώσε μου την σημασία της λέξης wall\n");
leg = input("Δώσε μου την σημασία της λέξης leg\n");
while (leg != "πόδι"):
leg = input("Δώσε μου την σημασία της λέξης leg\n");
if(leg == "πόδι"):
print("Σωστό");
score = score + 1
break;
else: print("Λάθος");
leg = input("Δώσε μου την σημασία της λέξης leg\n");
Posts: 1,298
Threads: 38
Joined: Sep 2016
(Jul-07-2017, 05:15 PM)Basilis Wrote: firstly i put lot of parenthesis and semi-colons bc its more familiar to me due to javascript! No, firstly this is Python not javascript. If you want to write javascript, than do so, otherwise follow the rules for Python. If you don't you are only going to run into problems, not to mention you're not going to get much help here if you don't show some effort.
Look at the code I gave you (which works) and compare it to the rest of your 'spaghetti' code (which doesn't). By the way, you should have gotten an error at line 8 since you never defined the variable "score" before using it, which you should have posted in it's entirety (between the 'error' tags)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 1
Joined: Jul 2017
is there a case that the parenthesis and the semi colons will prompt an error ?
Posts: 3,458
Threads: 101
Joined: Sep 2016
Syntactically? No.
But there could be misunderstandings if you show your code to other people. And that's a sort of error.
If "your style" is to have semicolons at the end of each statement, why aren't you at least doing that consistently?
Why have a multi-line if block followed by an in-line else block? To me, that's much worse than semicolons.
|