Probably my coding isn’t so good and some lines wouldn’t make much sense or are not necessary, but the code purpose is dead-simple:
I want to create a function that uses an input(string), and convert it as an integer, which is going to be use in a math problem.
Plus: I want my code to interpret a random generated number and print it as its string equivalent:
### 'one' --> 1
### 'zero' --> 0
import random
##'one' == 1
##'zero' == 0
def name_to_number(name):
if name == 'one':
return 1
def number_to_name(comp_number):
if comp_number == 1:
return 'one'
def lit_for_num(name):
'''(str) -> str'''
comp_number = random.randrange(0,1)
equation = (abs(comp_number - int(name)))
if equation == 0:
print('Hallo!')
return 'Computer draws' + comp_number
else:
return 'Computer draws 0'
Any help is very much thanked.
The first problem is that you seem to be using
oneas input, and henceint('one')will give you that error.Secondly, in:
the
elseclause will always be called becausecomp_numberis always 0.rand.randrangeis similar tochoice(range(start, stop, step)), which meansrandrange(0,1)will always return 0. You would wantrandrange(0,2)instead if you want to either0or1. Or, userandom.randint(0,1)instead, which will include end points0and1.As a bonus, to process text number to number, you may want to consider text2num written by Greg Hewgill.