2017-03-22 6 views
-1

私はpythonで数字を数字の文字バージョンに変換しようとしました。例:323 - > 323 私が抱えている問題は、プロセスが完了して出力が表示されたときに、変数の1つが正しい値を表示していないことです。所望の出力は次のようになります。変数がPythonで正しい値を格納していません

Enter a number under 999: 323 
323 -> three hundred twenty-three 

を代わりに次のようになります。誰も私がこれを理解することができた場合

Enter a number under 999: 323 
23 -> three hundred twenty-three 

、私はそれを大幅に感謝。ここに私のコードは次のとおりです。パイソン2インタプリタを使用しているコードはそれにいくつかの奇妙な側面を持っている理由ですのでところで

from __future__ import print_function, division 
import sys 
input = raw_input 

n = int(input("Enter a number under 999: ")) 

if n >= 999: 
print("Well, you didn't follow directions.") 
sys.exit(1) 

word = "" 
hundred = n//100 
if hundred == 1: 
    word += "one hundred" 
elif hundred == 2: 
    word += "two hundred" 
elif hundred == 3: 
    word += "three hundred" 
elif hundred == 4: 
    word += "four hundred" 
elif hundred == 5: 
    word += "five hundred" 
elif hundred == 6: 
    word += "six hundred" 
elif hundred == 7: 
    word += "seven hundred" 
elif hundred == 8: 
    word += "eight hundred" 
elif hundred == 9: 
    word += "nine hundred" 
if hundred > 0: 
    word += " " 
n = n%100 
if n == 10: 
    word += ' ten' 
elif n == 11: 
    word += ' eleven' 
elif n == 12: 
    word += ' twelve' 
elif n == 13: 
    word += ' thirteen' 
elif n == 14: 
    word += ' fourteen' 
elif n == 15: 
    word += ' fifteen' 
elif n == 16: 
    word += ' sixteen' 
elif n == 17: 
    word += ' seventeen' 
elif n == 18: 
    word += ' eighteen' 
elif n == 19: 
    word += ' nineteen' 
else: 
    ones = n%10 
    tens = n//10 
    if tens == 2: 
     word += "twenty" 
    elif tens == 3: 
     word += "thirty" 
    elif tens == 4: 
     word += "fourty" 
    elif tens == 5: 
     word += "fifty" 
    elif tens == 6: 
     word += "sixty" 
    elif tens == 7: 
     word += "seventy" 
    elif tens == 8: 
     word += "eighty" 
    elif tens == 9: 
     word += "ninety" 

    if tens > 0 and ones > 0: 
     word += '-' 

    if ones == 1: 
     word += 'one' 
    elif ones == 2: 
     word += 'two' 
    elif ones == 3: 
     word += 'three' 
    elif ones == 4: 
     word += 'four' 
    elif ones == 5: 
     word += 'five' 
    elif ones == 6: 
     word += 'six' 
    elif ones == 7: 
     word += 'seven' 
    elif ones == 8: 
     word += 'eight' 
    elif ones == 9: 
     word += 'nine' 

print("{} -> {}".format(n, word)) 

ああと、私のクラスは、Python 3を学んでいます。

+7

「n = n%100」は、「323」を「23」に変更します。 – Barmar

答えて

1

n = n%100 

変異する "N" 以来、最初の "n" の値を "保存" する必要があります。最も簡単な方法は、次のようになります。

別の変数

n = int(input("Enter a number under 999: ")) 

セットを宣言した後のn

n = int(input("Enter a number under 999: ")) 
num = n 

その後、交換する

print("{} -> {}".format(n, word)) 

print("{} -> {}".format(num, word)) 
+0

これは機能しました。ありがとうございました! – jackmasterlooter

0

n = int(input("Enter a number under 999: ")) 
user_number = n 
(...) 
print("{} -> {}".format(user_number, word)) 

にあなたがn上で操作を行うたびに、あなたのコードを変更し

あなたはそれを変更します。

n = n%100 

だからそれ(user_number)を変更する前に別の変数に保存し、でそれを印刷終わり。

関連する問題