Advertisement
trodland

likning_generator

Oct 21st, 2020
1,956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import random
  2. import string
  3.  
  4. oppgavenavn = string.ascii_lowercase  # tekststreng med alle små bokstaver
  5. oppgavenr = 0
  6.  
  7.  
  8. while oppgavenr < len(oppgavenavn):
  9.     x = random.randint(-40,40)
  10.     v1 = random.randint(-40,40)
  11.     v2 = random.randint(-40,40)
  12.     v3 = random.randint(-40,40)
  13.     v4 = random.randint(-40,40)
  14.     v5 = random.randint(-40,40)
  15.          
  16.     vs = "v1*x + v2"
  17.     hs = "v3"
  18.    
  19.     # Eksempel på vanskeligere oppgaver
  20.     #vs = "v1/(v2*x)+v3"
  21.     #hs = "v4/v5"
  22.    
  23.     if x != 0 and v1 != 0 and v2 != 0 and v3 != 0 and v4 != 0 and v5 != 0 and v1 != v5 and eval(vs) == eval(hs):
  24.        
  25.         out = vs + " = " + hs
  26.        
  27.         out = out.replace("v1",str(v1))\
  28.                   .replace("v2",str(v2))\
  29.                   .replace("v3",str(v3))\
  30.                   .replace("v4",str(v4))\
  31.                   .replace("v5",str(v5))\
  32.                   .replace("*","")\
  33.                   .replace("1x","x")\
  34.                   .replace("- -","+ ")\
  35.                   .replace("+ -","- ")\
  36.                   .replace("--","+")\
  37.                   .replace("+-","-")\
  38.                   .replace("(","")\
  39.                   .replace(")","")
  40.        
  41.         print(oppgavenavn[oppgavenr],") ",out,sep="",end="\t")
  42.         oppgavenr += 1
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement