'Return Value from Function is None
Hello So i created a function to see if every character of a string is valid to be changed from base 10 to base 2(binary for example) but i dont know why everytime i call the function it return None. Here is my function (I tried 2 Method)
Version 1 ( created a value initialized with False and if in the loop one of the char is no into the base it should return false and if it loop through all the character change the value to True and return it):
def valide_nbr_base(p_base,p_nombre):
base_bin = [0,1]
base_oct = [0,1,2,3,4,5,6,7]
base_hex = [0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f"]
p_rep = False
if p_base == 2:
for i in range(len(p_nombre)):
if p_nombre[i] not in base_bin:
return r_rep
else:
p_rep = True
return p_rep
#if p_base == 8:
#if p_base == 16:
Version 2(Same as version 1 but with no variable simply return True or False):
def valide_nbr_base(p_base,p_nombre):
base_bin = [0,1]
base_oct = [0,1,2,3,4,5,6,7]
base_hex = [0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f"]
if p_base == 2:
for i in range(len(p_nombre)):
if p_nombre[i] not in base_bin:
return False
else:
return True
#if p_base == 8:
#if p_base == 16:
Here is the full code so you can have a idea of how it work:
#def base_to_decimal():
def valide_nbr_base(p_base,p_nombre):
base_bin = [0,1]
base_oct = [0,1,2,3,4,5,6,7]
base_hex = [0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f"]
p_rep = False
if p_base == 2:
for i in range(len(p_nombre)):
if p_nombre[i] not in base_bin:
return r_rep
else:
p_rep = True
return p_rep
#if p_base == 8:
#if p_base == 16:
from validerSaisieH22_Partie import *
#Comparaison Pour Valider Bonne Valeur
base = [2,8,16]
#Demander un choix de base si != int Value Error sinon boucle jusqu'a trouver la bonne base
choix =saisirEntier("Choix de la base 2 [binaire], 8 [octal] 16[Hexadécimal] :")
while choix not in base:
print("Erreur ==> Choix de la base doit être 2 ou 8 ou 16")
choix =saisirEntier("Choix de la base 2 [binaire], 8 [octal] 16[Hexadécimal] :")
#Demander le nombre a convertir et voir si il respecte le choix de la base si oui continuer sinon quitter le programme
nombre = input("Donner un nombre valide dans le système binaire pour le convertir en décimal:")
validiter = valide_nbr_base(base,nombre)
print(validiter)
the imported module is simply a try and except so people dont try to enter a letter at the base selection.
Thanks for the help i dont see why it would return None! :)
Solution 1:[1]
On Line 11 in your full code you have a undefined variable,
"r_rep" is not defined
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | WillBrobin |
