I'm trying to put together two functions and I can't for the life of me figure out what I've done wrong.
Function names() is supposed to repeatedly ask the user to input a name. When the user ceases to enter names, the program should print the amount of times each name was entered. This is what I've got so far.
def names(): counter={} name=input('Enter next name:') while name!='': if name in counter: counter[name]+=1 else: counter[name]=1 name=input('Enter next name:') for name in counter: if counter[name]>1: print('There are {1} students named {2}.'.format(counter[name], name)) else: print('There is {1} student named {2}.'.format(counter[name], name))
It works up until I stop inputing names. At that point, it just gives me a syntaxerror stating "unexpected EOF while parsing." Anyone know what I need to fix?
Function testCraps() is supposed to take a positive integer n as input and simulate n games of the dice game, craps. It should then return the user's win rate after n games of craps. Here, I've got:
def craps(): import random dice=random.randrange(1,7)+random.randrange(1,7) if dice in (7,11): return 1 elif dice in (2,3,12): return 0 else: newRoll=random.randrange(1,7)+random.randrange(1,7) while newRoll not in (7,dice): newRoll=random.randrange(1,7)+random.randrange(1,7) if newRoll==dice: return 1 else: return 0 def testCraps(n): count=0 for i in range(n): if craps()==1: count+=1 return count/n
For some reason, testCraps() always seems to give me 0, and I can't see any sort of problem with it. Can you see something I'm missing?
The problem with names()
is that when string formatting you should index starting at {0}
not {1}
. After correcting this and the indentation it works fine:
def names():
counter={}
name=input('Enter next name:')
while name!='':
if name in counter:
counter[name]+=1
else:
counter[name]=1
name=input('Enter next name:')
for name in counter:
if counter[name]>1:
print('There are {0} students named {1}.'.format(counter[name], name))
else:
print('There is {0} student named {1}.'.format(counter[name], name))
names()
Interactive session:
Enter next name:Galax
Enter next name:Test
Enter next name:Galax
Enter next name:
There is 1 student named Test.
There are 2 students named Galax.
Here's the craps code, with the indentation fixed and the casting to float suggested in one of the comments above:
def craps():
import random
dice=random.randrange(1,7)+random.randrange(1,7)
if dice in (7,11):
return 1
elif dice in (2,3,12):
return 0
else:
newRoll=random.randrange(1,7)+random.randrange(1,7)
while newRoll not in (7,dice):
newRoll=random.randrange(1,7)+random.randrange(1,7)
if newRoll==dice:
return 1
else:
return 0
def testCraps(n):
count=0
for i in range(n):
if craps()==1:
count+=1
return float(count)/n
print(testCraps(100))
Sample Output:
0.44
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.