Python cực cơ bản nhưng code em sai em không biết tại sao :( mong anh chị giúp đỡ với ạ :(



  • Đây là đoạn code cơ bản chỉ gồm print và variables = value. Em viết các dòng code y hệt nhau mà em không hiểu tại sao đến đoạn print"Exercise 4..." là nó bảo lỗi dù em viết y hệt những cái print"Exercise 3...",v.v
    Anh chị check sớm giúp em với ạ :( Em cảm ơn rất nhiều ạ :(
    @Không-Ai

    print "Complete the sentences in Reported Speech."
    print "Group 1"
    print "Exercise 1: \'Open the door,\' he said to them. "
    exercise1 = raw_input ("He told them... ")
    print "Exercise 2: \'Where are you going?\' he asked her."
    exercise2 = raw_input("He asked her where... ")
    print "Exercise 3:\'Which way did they go?\' he asked."
    exercise3 = raw_input("He asked... ")
    print "Exercise 4:\'Bring it back if it doesn’t fit\', I said to her."
    exercise4 = raw_input("I told... ")
    print "Exercise 5: \'Don’t try to open it now,\' she said to us."
    exercise5 = raw_input ("She told... ")
    print "Group 2"
    print "Exercise 6: \'Is it going to be a fine day today?\' I asked her"
    exercise6 = raw_input ("I asked her... ")
    print "Exercise 7: \'He’s not at home\', she said."
    exercise7 = raw_input ("She said that... ")
    print "Exercise 8: \'Is the bus station far away?\' the girl asked."
    exercise8 = raw_input ("The girl wanted to know... ")
    print "Exercise 9: \'Don’t stay out late, Ann\' Tom said."
    exercise9 = raw_input ("Tom told Ann... ")
    print "Exercise 10: \'Please let me borrow your car,\' he said to her."
    exercise10 = raw_input ("He asked... ")
    
    
    


  • Em bị dính lỗi dấu nháy nhá ví dụ doesn't thì phải là doesn't. Đây là code của em a đã sửa lại

    print "Complete the sentences in Reported Speech."
    print "Group 1"
    print "Exercise 1: \'Open the door,\' he said to them. "
    exercise1 = raw_input ("He told them... ")
    print "Exercise 2: \'Where are you going?\' he asked her."
    exercise2 = raw_input("He asked her where... ")
    print "Exercise 3:\'Which way did they go?\' he asked."
    exercise3 = raw_input("He asked... ")
    print "Exercise 4:\'Bring it back if it doesn\'t fit\', I said to her."
    exercise4 = raw_input("I told... ")
    print "Exercise 5: \'Don\'t try to open it now,\' she said to us."
    exercise5 = raw_input ("She told... ")
    print "Group 2"
    print "Exercise 6: \'Is it going to be a fine day today?\' I asked her"
    exercise6 = raw_input ("I asked her... ")
    print "Exercise 7: \'He\'s not at home\', she said."
    exercise7 = raw_input ("She said that... ")
    print "Exercise 8: \'Is the bus station far away?\' the girl asked."
    exercise8 = raw_input ("The girl wanted to know... ")
    print "Exercise 9: \'Don\'t stay out late, Ann\' Tom said."
    exercise9 = raw_input ("Tom told Ann... ")
    print "Exercise 10: \'Please let me borrow your car,\' he said to her."
    exercise10 = raw_input ("He asked... ")
    


  • @Không-Ai Anh ơi em làm được rồi ạ nhưng không hiểu sao hàm ex7() dù em nhập sai vào hay không nhập gì nó vẫn cứ trả về TRUE thế ạ :( em không hiểu tại sao ạ :(

    0_1471361764723_Screen Shot 2016-08-16 at 22.35.46.png



  • @Không-Ai à em quên mất gửi anh đoạn code của em ạ :( vấn đề nằm ở phần exercise 7 mà em không hiểu em sai ở đâu ạ :( dù gõ gì nó cũng trả về "True." ạ :(

    print "Group 1"
    print "Exercise 1: \'Open the door,\' he said to them. "
    exercise1 = raw_input ("He told them... ")
    
    print "Exercise 2: \'Where are you going?\' he asked her."
    exercise2 = raw_input("He asked her where... ")
    
    print "Exercise 3:\'Which way did they go?\' he asked."
    exercise3 = raw_input("He asked... ")
    
    print "Exercise 4:\'Bring it back if it doesn\'t fit\', I said to her."
    exercise4 = raw_input("I told... ")
    
    print "Exercise 5: \'Don\'t try to open it now,\' she said to us."
    exercise5 = raw_input ("She told... ")
    
    print "Group 2"
    print "Exercise 6: \'Is it going to be a fine day today?\' I asked her"
    exercise6 = raw_input ("I asked her... ")
    
    print "Exercise 7: \'He\'s not at home\', she said."
    exercise7 = raw_input ("She said that... ")
    
    print "Exercise 8: \'Is the bus station far away?\' the girl asked."
    exercise8 = raw_input ("The girl wanted to know... ")
    
    print "Exercise 9: \'Don\'t stay out late, Ann\' Tom said."
    exercise9 = raw_input ("Tom told Ann... ")
    
    print "Exercise 10: \'Please let me borrow your car,\' he said to her."
    exercise10 = raw_input ("He asked... ")
    def ex1():
    	if exercise1 == "to open the door." or exercise1 == "to open the door":
    		return "True."
    	else:
    		return "False. Your answer is \'He told them %s\' but the correct answer is \'He told them to open the door.\'" % (exercise1)
    def ex2():
    	if exercise2 == "she was going." or exercise2 == "she was going":
    		return "True."
    	else:
    		return "False. Your answer is \'He asked her where %s\' but the correct answer is \'He asked her where she was going.\'" % (exercise2)
    def ex3():
    	if exercise3 == "me which way they had gone." or exercise3 == "me which way they had gone":
    		return "True."
    	else:
    		return "False. Your answer is \'He asked %s \' but the correct answer is \'He asked me which way they had gone. \'" % (exercise3)
    def ex4():
    	if exercise4 == "her to bring it back if it didn\'t fit." or exercise4 == "her to bring it back if it didn\'t fit":
    		return "True."
    	else:
    		return "False. Your answer is \'I told %s \' but the correct answer is \'I told her to bring it back if it didn\'t fit. \'" % (exercise4)
    def ex5():
    	if exercise5 == "us not to try to open it then." or exercise5 == "us not to try to open it then":
    		return "True."
    	else:
    		return "False. Your answer is \'She told %s \' but the correct answer is \'She told us not to try to open it then. \'" % (exercise5)
    def ex6():
    	if exercise6 == "whether it was going to be a fine day that day." or exercise6 == "if it was going to be a fine day that day." or exercise6 == "whether it was going to be a fine day that day" or exercise6 =="if it was going to be a fine day that day":
    		return "True."
    	else:
    		return "False. Your answer is \' I asked her %s\' but the correct answer is \'I asked her whether/if it was going to be a fine day that day. \'" % (exercise6)
    def ex7():
    	if exercise7 == "he was not at home." or "he was not at home":
    		return "True."
    	else:
    		return "False. Your answer is \' She said that %s\' but the correct answer is \'She said that he was not at home. \'" % (exercise7)
    def ex8():
    	if exercise8 == "whether the bus station was far away." or exercise8 == "if the bus station was far away." or exercise8 == "whether the bus station was far away" or exercise8 == "if the bus station was far away":
    		return "True."
    	else:
    		return "False. Your answer is \'The girl wanted to know %s \' but the correct answer is \'The girl wanted to know whether/ if the bus station was far away. \'" % (exercise8)
    def ex9():
    	if exercise9 == "not to stay out late." or exercise9 == "not to stay out late":
    		return "True."
    	else:
    		return "False. Your answer is \'Tom told Ann %s \' but the correct answer is \' Tom told Ann not to stay out late.\'" % (exercise9)
    def ex10():
    	if exercise10 == "her to let him borrow her car." or exercise10 == "her to let him borrow her car":
    		return "True."
    	else:
    		return "False. Your answer is \'He asked %s \' but the correct answer is \' He asked her to let him borrow her car.\'" % (exercise10)
    print "Group 1"
    print "Exercise 1 is %s" % (ex1())
    print "Exercise 2 is %s" % (ex2())
    print "Exercise 3 is %s" % (ex3())
    print "Exercise 4 is %s" % (ex4())
    print "Exercise 5 is %s" % (ex5())
    print "Group 2"
    print "Exercise 6 is %s" % (ex6())
    print "Exercise 7 is %s" % (ex7())
    print "Exercise 8 is %s" % (ex8())
    print "Exercise 9 is %s" % (ex9())
    print "Exercise 10 is %s" % (ex10())```


  • def ex7():
    	if exercise7 == "he was not at home." or "he was not at home":
    		return "True."
    	else:
    		return "False. Your answer is \' She said that %s\' but the correct answer is \'She said that he was not at home. \'" % (exercise7)
    

    em ghi là or "he was not at home": nên if luôn luôn đúng.
    Phải sửa lại là:

    def ex7():
    	if exercise7 == "he was not at home." or exercise7 == "he was not at home":
    		return "True."
    	else:
    		return "False. Your answer is \' She said that %s\' but the correct answer is \'She said that he was not at home. \'" % (exercise7)
    


  • @Dung-Nguyen à hoá ra là vậy :p anh ơi ý tưởng của em viết như thế này vẫn chạy được. Nhưng em nghe nói python là ngôn ngữ lập trình ngắn chứ không dài loằng ngoằng như em làm. Em mới học xong phần cơ bản Dictionaries và List vậy áp dụng như thế nào để làm cái bài như trên kia ạ bằng cách ngắn hơn với Dict và List ạ? Em cảm ơn anh ạ :)



  • @Quốc-Phong Anh cũng mới ở mức cơ bản nên đây là cách duy nhất a nghĩ ra để sử dụng List con Diction thì thấy lòng vòng nên thôi :D

    Q1=["Exercise 1: 'Open the door,' he said to them.\nHe told them... ",
    	"Exercise 2: 'Where are you going?' he asked her.\nHe asked her where... ",
    	"Exercise 3:'Which way did they go?' he asked.\nHe asked... ",
    	"Exercise 4:'Bring it back if it doesn't fit', I said to her.\nI told... ",
    	"Exercise 5: 'Don't try to open it now,' she said to us.\nShe told... "]
    Q2=["Exercise 6: 'Is it going to be a fine day today?' I asked her\nI asked her... ",
    	"Exercise 7: 'He's not at home', she said.\nShe said that... ",
    	"Exercise 8: 'Is the bus station far away?' the girl asked.\nThe girl wanted to know... ",
    	"Exercise 9: 'Don't stay out late, Ann' Tom said.\nTom told Ann... ",
    	"Exercise 10: 'Please let me borrow your car,' he said to her.\nHe asked... "]
    
    T1=[["to open the door." ,"to open the door"],
    	["she was going.","she was going"],
    	["me which way they had gone.","me which way they had gone"],
    	["her to bring it back if it didn't fit.","her to bring it back if it didn't fit"],
    	["us not to try to open it then.","us not to try to open it then"]]
    T2=[["whether it was going to be a fine day that day.","if it was going to be a fine day that day.","whether it was going to be a fine day that day","if it was going to be a fine day that day"],
    	["he was not at home.","he was not at home"],
    	["whether the bus station was far away.","if the bus station was far away.","whether the bus station was far away","if the bus station was far away"],
    	["not to stay out late.","not to stay out late"],
    	["her to let him borrow her car.","her to let him borrow her car"]]
    F1=[["False. Your answer is 'He told them","' but the correct answer is 'He told them to open the door.'"],
    	["False. Your answer is 'He asked her where","' but the correct answer is 'He asked her where she was going.'"],
    	["False. Your answer is 'He asked","' but the correct answer is 'He asked me which way they had gone. '"],
    	["False. Your answer is 'I told ","'but the correct answer is 'I told her to bring it back if it didn't fit.'"],
    	["False. Your answer is 'She told ","' but the correct answer is 'She told us not to try to open it then. '"]
    	]
    F2=[["False. Your answer is 'I asked her ","' but the correct answer is'I asked her whether/if it was going to be a fine day that day.'"],
    	["False. Your answer is ' She said that ","' but the correct answer is 'She said that he was not at home. '"],
    	["False. Your answer is 'The girl wanted to know ","' but the correct answer is 'The girl wanted to know whether/ if the bus station was far away. '"],
    	["False. Your answer is 'Tom told Ann "," ' but the correct answer is ' Tom told Ann not to stay out late.'"],
    	["False. Your answer is 'He asked ","' but the correct answer is ' He asked her to let him borrow her car.'"]]
    A1=[]
    A2=[]
    def check(Q,A,T,F):
    	for x in Q:
    		A.append(raw_input(x))
    	O=[]
    	for i in range(len(A)):
    		if A[i] in T[i]:
    			O.append('True')
    		else:
    			s=F[i][0]+" "+A[i]+F[i][1]
    			O.append(s)
    	return O
    print "Group 1"
    p1=check(Q1,A1,T1,F1)
    print "Group 2"
    p2=check(Q2,A2,T2,F2)
    print "Group 1"
    for x in p1:
    	print "Exercise"+str(p1.index(x)+1)+" is "+x
    print "Group 2"
    for y in p2:
    	print "Exercise" + str(p2.index(y)+6) + " is " + y
    


  • @Không-Ai Em cảm thấy nó khoa học hơn hẳn ạ cơ mà e k hiểu cho lắm ạ :(



  • @Không-Ai mà anh bảo anh cũng đang học cơ bản ạ? Anh học ở đâu ạ? Anh học bao lâu r ạ? Mục tiêu học xong python cơ bản là gì vậy ạ? Anh chia sẻ với em được không ạ? Em cảm ơn anh



  • @Quốc-Phong A tự học thA học linh tinh lắm chuyên ngành của anh là sinh học, sau đó qua tâm đến tin sinh nên a mày mò học giải quyết một vài bài toán sinh học, sau đó thấy hay nên hiện h muốn tìm hiểu tiếp nên đang dừng lại để đọc sách



  • @Không-Ai a đọc sách chuyên sâu về python ấy ạ? Anh có email hay facebook gì đó không ạ ae mình có thể giữ liên hệ để tiện trao đổi đc k ạ? Em cảm ơn anh



  • @Quốc-Phong Cũng không phải là chuyển sâu. Trước nóng vọi cứ lao vào code nhưng thấy mình code của mình không được như người ta nên anh đnag quét lại hết xem cái gì biết rồi cái gì chưa :D



  • @Không-Ai Anh có fb k ạ ?



  • @Quốc-Phong Facebook anh là Không Ai avatar tương tự như ở đây



  • @Không-Ai a cho e cái link đc k ạ e tìm k thấy ạ




Hãy đăng nhập để trả lời
 

Có vẻ như bạn đã mất kết nối tới Cộng đồng Python Việt Nam, vui lòng đợi một lúc để chúng tôi thử kết nối lại.