Python Forum
name ' ' is not defined error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name ' ' is not defined error
#1
[Image: AOhTfvC]


it won't print my str please help it has worked before but just now it won't work
Reply
#2
Copy and paste your code using python tags, and put the full text of the error message in output tags.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
import math

while True:  
    try:
        tal = int(input("tal?: "))
    except ValueError:
        print("kun tal")
    n = tal
    for p in range(2,n):
        moudu = n%p
        prime = "det er et primtal"
        if moudu == 0:
            prime = "det er ikke et primtal"
            break
    ianden = tal*tal
    print (prime)
    print ("² = ", ianden)
    print ("kvadratrod =", math.sqrt(tal))
    
    input('Press enter to continue: ')
Output:
Exception has occurred: NameError name 'prime' is not defined File "C:\Users\nonoe\OneDrive\Skrivebord\info om tal 0.1.py", line 20, in <module> print (prime)
Reply
#4
The variable prime only gets defined if a value greater than 2 is entered. If you enter 1 on the first try, the loop on line 9 never executes, and prime never gets defined. Note that if you enter 1 on the second try, it uses the value of prime from the previous try. You need to set a value to prime before that loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
thank you very much.
this really helped my day :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,124 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,350 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,608 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,378 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,180 Dec-14-2020, 05:45 PM
Last Post: karabakh
  name error "name"is not defined MaartenRo 1 3,425 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 2,278 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 3,844 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 5,031 May-24-2020, 06:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020