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!

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
15 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Krabat693
6 months ago

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.

Krabat693
6 months ago
Reply to  Wolkenfall123

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.

Krabat693
6 months ago

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 ̄\_(ツ)_/ ̄

Krabat693
6 months ago

I see that the last if-else query is still a tab stop, so that they are skipped in case of control=false.

Krabat693
6 months ago
from math import* 
antwort=True
zahl=input("Geben Sie eine natuerliche Zahl ein")
teiler=2
control=False

if isinstance(zahl, int):
  if zahl<=1:
    print(str(zahl)+ " ist keine natuerliche Zahl!")
  else:
    control=True
else:
  print(str(zahl)+ " ist keine Zahl!")
if control==true:
  while zahl>=2 and teiler<= sqrt(zahl):
    if zahl % teiler==0:
      antwort=False
      break
    teiler=teiler + 1
   
if antwort==True:
  print("Die Zahl " +str(zahl)+" ist eine Primzahl.")
else:
  print("Die Zahl "+str(zahl) + " ist keine Primzahl.")

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.

Krabat693
6 months ago

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.

cleanercode
6 months ago
zahl = int(input("Zahl eingeben: "))

input() always returns a string. So you have to cast explicitly after int.

McSKB
6 months ago

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.

McSKB
6 months ago
Reply to  Wolkenfall123

I don’t use the language. Look at the API documentation.