2017-07-06 30 views
-6
value = [10 ,20 ,50 ,100] 
Balance = 0 
Change = 0 
totalCoins = 0 
print("|~~~~~~~~~|") 
print("|~~~Jack~~|") 
print("|~~Blacks~|") 
print("|~~Food~~~|") 
print("|~~~And~~~|") 
print("|~~Drink~~|") 
print("|~Machine~|") 

print("Row 1 ~ Crisps") 
print("A1 is Walkers Crisps") 
print("B1 is Pringles") 
print("C1 is Lays") 
print("Row 2 ~ Drinks") 
print("A2 is Diet Coke") 
print("B2 is Crystal Pepsi") 
print("C2 is Sprite") 
print("Row 3 ~ Sweets") 
print("A3 is Cadbury's Chocolate") 
print("B3 is Strawberry Laces") 
print("C3 is Haribo") 
CoinAmount = input("Would you like to continue, Type Yes to continue or No to stop") 
if CoinAmount == "Yes" or "YES": 
    Balance = int(input("Enter the total value of your Coins in P, Note: Machine only accepts 10p, 20p, 50p £1coins however you Must put the amount you want in Pennies either 10, 20, 50, 100,: ")) 
    totalCoins = Balance + totalCoins 
    print ('You now have a total Balance of' ,totalCoins ,'P') 
    CoinAmount = input("Would you like to continue, Type Yes to continue or No to stop") 
elif Balance != value: 
    print("Machine doesn't accept these total coins") 
while CoinAmount == "Yes": 
    Balance = int(input("Enter the total value of your Coins in P, Note: Machine only accepts 10p, 20p, 50p £1coins however you Must put the amount you want in Pennies either 10, 20, 50, 100,: ")) 
    totalCoins = totalCoins + Balance 
    print ('You now have a total Balance of' ,totalCoins ,'P') 
    CoinAmount = input("Would you like to continue, Type Yes to continue or No to stop ") 
while CoinAmount != "Yes": 
    break 
choice = int(input("Row 1 are crisps they cost 40p, Row 2 are drinks they cost 60p and Row 3 are sweets they cost £1 Choose a row: ")) 

if choice == 1: 
    row1 = input("Please choose either A1, B1 or C1 for your item: ") 
if row1 == "A1": 
    print('You have bought A Packet of Walkers Crisps for 40p, you now have' , (totalCoins - 40), 'P left') 
    Change = totalCoins - 40 
    Credit = input("Would you like to continue, Type Yes to continue or No to stop ") 
elif row1 == "A2": 
    print('You have bought a tube of Pringles for 40p, you now have' , (totalCoins - 40), 'P left') 
    Change = totalCoins - 40 
    Credit = input("Would you like to continue, Type Yes to continue or No to stop ") 
elif row1 == "A3": 
    print('You have bought a packet of Lays for 40p, you now have' , (totalCoins - 40), 'P left') 
    Change = totalCoins - 40 
    Credit = input("Would you like to continue, Type Yes to continue or No to stop ") 
elif choice == 1 and totalCoins < 40: 
    print('You cannot buy anything else please take your change of:' ,Change ,'P') 


if choice == 2: 
    row2 = input("Please choose either A2, B2, C3 for your item: ") 
elif row2 == "A2": 
    print('You have bought a can of Diet Coke for 60p and you now have', (totalCoins - 60), 'P left') 
    Change = totalCoins - 60 
    Credit = input ("Would you like to continue, Type Yes to continue or No to stop ") 
elif row2 == "B2": 
    print('You have bought a bottle of Crystal Pepsi for 60p and you now have', (totalCoins - 60), 'P left') 
    Change = totalCoins - 60 
    Credit = input ("Would you like to continue, Type Yes to continue or No to stop ") 
elif row2 == "C2": 
    print('You have bought a bottle of Sprite for 60p and you now have', (totalCoins - 60), 'P left') 
    Change = totalCoins - 60 
    Credit = input ("Would you like to continue, Type Yes to continue or No to stop ") 
elif choice == 2 and totalCoins < 60: 
    print ('You cannot buy this please choose something else') 
    choice = int(input("Row 1 are crisps they cost 40p, Row 2 are drinks they cost 60p and Row 3 are sweets they cost £1 Choose a row: ")) 


if choice == 3: 
    row3 = input("Please Choose Either A3, B3, and C3 for your item: ") 
elif row3 == "A3": 
    print('You have bought a bar of Cadburys Chocolate for £1 and now have' , (totalCoins - 100), 'P left') 
    Change = totalCoins - 100 
    Credit = input("Would you like to continue, Type Yes to continue or No to stop ") 
elif row3 == "B3": 
    print('You have bought a packet of Strawberry Laces for £1 and now have' , (totalCoins - 100), 'P left') 
    Change = totalCoins - 100 
    Credit = input("Would you like to continue, Type Yes to continue or No to stop ") 
elif row3 == "B3": 
    print('You have bought a packet of Haribo for £1 and now have' , (totalCoins - 100), 'P left') 
    Change = totalCoins - 100 
    Credit = input("Would you like to continue, Type Yes to continue or No to stop ") 
elif choice == 3 and totalCoins < 100: 
    print("You cannot afford this please choose something else") 

我有我的行在Python3定義的輸入,但如果我例如決定選擇3我的選擇,然後說,我沒有在我的代碼早些時候定義row1。我如何確保我的Python代碼在不同的代碼段下注冊row1 2或3?

我不確定我沒有做什麼來定義它,因爲它使用輸入來對它進行分類。

回答

0

問題是因爲您沒有正確縮進您的代碼。 Python代碼依靠正確的縮進來確定命令的處理順序。目前,即使您沒有選擇row1,您的代碼也會檢查row1的值,但是您可以根據從輸入中選擇row1來定義row1。您需要在每個選擇選項下縮進if語句,如下所示。

if choice == 1: 
    row1 = input("Please choose either A1, B1 or C1 for your item: ") 
    if row1 == "A1": 
     //do something 
    elif row1 == 'A2': 
     //do something else etc... 
+0

你不應該在2和3你的「如果選擇」後使用ELIF,看你這樣做是爲了選擇1的方式,它改變爲,並按照上述 – Deengus

+0

壓痕還你不檢查這筆錢恰當,它有可能無錢買東西。請記住,大多數語言如果發現他們發現的第一個真實陳述,就會退出,所以你應該首先檢查錢。 – Deengus

相關問題