Simplifying this Python code for RPG levels -


i making simple rpg game characters level based on experience (represented x in code below) in exponential fashion. levels go level 10, confused if experience falls in-between exponential numbers- wrote code below round levels down lowest experience level, there way make code simpler or more efficient?

  if 2<x<4:         level=1     if 4<x<8:         level=2     if 8<x<16:         level=3     if 16<x<32:         level=4     if 32<x<64:         level=5     if 64<x<128:         level=6     if 128<x<256:         level=7     if 256<x<512:         level=8     if 512<x<1024:         level=9     if x>1024:         level=10     print (level) 

from math import log  ...  level = int(log(x, 2)) level = min(level, 10) # suggested morb print(level) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -