按倍数递减? ; "do nothing";创建除法或类似除法的算法
de-increment by multiples? ; "do nothing"; create a division or division-like algorithm
仍在解决啤酒问题。从头开始。我尽量不买一半的啤酒,所以这是我开始的地方:
#import math
def buybeers(wallet):
beers = 0
for(int in xrange(0,wallet,2)) #xrange not defined
beers += 1
wallet -= 2
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet))
我正在想办法用 2 美元和 return 奇数美元买一瓶啤酒。
第二次尝试,试图找出逻辑错误:
def buybeers(wallet):
beers = 0
for i in range(1,wallet,3):
beers += 1
wallet -= 3
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet))
当我有 14 美元时,问题出现了。我最终得到 -1 美元和 5 瓶啤酒(我不应该喝啤酒,还欠了 1 美元)。虽然没错,但有些便利店不接受标签。
最终提交(啤酒 3 美元)
def buybeers(wallet):
beers = 0
for i in range(1,wallet,3):
if wallet >= 3: #if you got enough for a beer, buy one.
beers += 1
wallet -= 3
#else:
# pass #otherwise, "do nothing"/pass (it seems this is automated).
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet))
你少了几个括号,你的逻辑是错误的。假设你有 </code> 并且每瓶啤酒花费 <code>
然后你可以有 2
啤酒并剩下 </code>,但是,你的代码会给你 <code>3
啤酒,现在你又欠了一美元。您可以像这样使用除法和模数来简化它
def buybeers(wallet):
beers = wallet//2
wallet = wallet % 2
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet)) #5 as input
(2, 1)
只需修复您遇到的语法错误
def buybeers(wallet):
beers = 0
for i in xrange(0,wallet,2):
beers += 1
wallet -= 2
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet)) #5 as input
(3, -1)
由于您的 for 循环逻辑已关闭,因此不会为您提供正确的输出。
仍在解决啤酒问题。从头开始。我尽量不买一半的啤酒,所以这是我开始的地方:
#import math
def buybeers(wallet):
beers = 0
for(int in xrange(0,wallet,2)) #xrange not defined
beers += 1
wallet -= 2
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet))
我正在想办法用 2 美元和 return 奇数美元买一瓶啤酒。
第二次尝试,试图找出逻辑错误:
def buybeers(wallet):
beers = 0
for i in range(1,wallet,3):
beers += 1
wallet -= 3
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet))
当我有 14 美元时,问题出现了。我最终得到 -1 美元和 5 瓶啤酒(我不应该喝啤酒,还欠了 1 美元)。虽然没错,但有些便利店不接受标签。
最终提交(啤酒 3 美元)
def buybeers(wallet):
beers = 0
for i in range(1,wallet,3):
if wallet >= 3: #if you got enough for a beer, buy one.
beers += 1
wallet -= 3
#else:
# pass #otherwise, "do nothing"/pass (it seems this is automated).
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet))
你少了几个括号,你的逻辑是错误的。假设你有 </code> 并且每瓶啤酒花费 <code>
然后你可以有 2
啤酒并剩下 </code>,但是,你的代码会给你 <code>3
啤酒,现在你又欠了一美元。您可以像这样使用除法和模数来简化它
def buybeers(wallet):
beers = wallet//2
wallet = wallet % 2
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet)) #5 as input
(2, 1)
只需修复您遇到的语法错误
def buybeers(wallet):
beers = 0
for i in xrange(0,wallet,2):
beers += 1
wallet -= 2
return beers, wallet
wallet = int(input("How many wallet do you have?"))
print(buybeers(wallet)) #5 as input
(3, -1)
由于您的 for 循环逻辑已关闭,因此不会为您提供正确的输出。