Wieso funktioniert dieses Programm nicht?
Brauche Hilfe bei diesem Programm. Es soll Fehlerhafte Eingaben wie Texte abweisen, jedoch kommt bei Zeile 19 immer die Fehlermeldung:„x muss vom Typ Zahl sein“ wenn man einen Text eingibt. Das Programm ist auf WebTigerJython.
Vielen Dank schonmal für die Hilfe!
You initialized the response as TRUE, but when entering a text, the variable is not changed to FALSE.
and since answer=true begins to count your program to then determine that it cannot count on a string.
So would I have to add an answer=False?
Yeah, that would solve your problem.
But, good practice you should remember the for sure future:
If you have a Bool variable that decides whether a critical part of your program is running or not, then initialize it as FALSE.
Thus, your program is automatically aborted and only when the critical condition is fulfilled, it is set to true.
All a question of exercise, at some point it makes click and then you understand what you write.
… then you can eventually correct programs whose programming language you see for the first time ̄\_(ツ)_/ ̄
Thank you very much! I was really frustrated with this program, but now I understand it better. Next time, I also intend to initialize the variable from the start with False.
I see that the last if-else query is still a tab stop, so that they are skipped in case of control=false.
I have another problem with the issue. If the entered number is <=1 or a text is entered, 2 sets are output.
I didn't control your Mathematical review now, but I added the variable control and converted the input control to a nested IF query.
if therefore the variable number contains a number, it is checked whether this is less than 1 and only when everything fits, the variable control is set to true and only when it is executed to true the loop is executed.
This is the text as in the picture (without editing)
from math import*
Answer=True
number=input(“At a natural number”)
Partial =2
if isinstance(number, int):
Answer=True
other:
Answer=False
print(str(number)+ ” is not a natural number!”)
if number <=1:
Answer=False
while number>=2 and part <= sqrt(number):
if number % part==0:
Answer= False
break
Partial += 1
if answer=True:
print(“The number “+str(number)+” is a prime number”)
other:
print(“The number “+str(number) + ” is not a prime number”)
Correction: the variable response has nothing to do with your input control, because I was confused because you change the variable in your input control.
If you can copy your code in as text, I’ll do it.
input() always returns a string. So you have to cast explicitly after int.
I think you need to check here whether the text that has been entered is a number, because a>0 or similar will not work.
How would I check that? So what order can I use for it?
I don’t use the language. Look at the API documentation.