Python Forum
If 2nd input in the game is correct, replace with 1st input and loop the game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If 2nd input in the game is correct, replace with 1st input and loop the game
#1
Trying to write a simple game to improve my Python knowledge as a newbie.

The game starts with asking a first country name Then asking another country name starts with the end letter of first one. If correct, assign second data as first and loop the game till wrong answer or user wants to quit.

The question is: how to assign the 2nd data as the 1st data and loop the game? Below code does not run as I want. Doesnt matter if 2nd input is correct or not, it just prints 'you lost!' and exits.

first_data=input('enter a country name: ')
print(first_data)
last_letter=str(first_data[-1])
print(last_letter)
second_data=input('enter a country name starts with the last letter of previous country name: ')

while last_letter==second_data[0]:
    #this is where I want to add the loop. If correct, then use the second data as first one and re-run the code from beginning
    second_data=first_data
    print(first_data)

print('you lost!')
Output:
enter a country name: nigeria nigeria a enter a country name starts with the last letter of previous country name: albania nigeria you lost! Process finished with exit code 0
Reply
#2
There is no condition for the you lost line. It will always print.
you will need something like
if second_data == first_data:
    # continue with whatever
else:
    print('you lost')
On a side note that should probably be in the while loop


Here is an example for you to play around with

while True:
    print('ctrl c to quit')
    first = input('country name: ')
    second = input('another name: ')

    last_letter = first[-1]
    first_letter = second[:1]

    if last_letter == first_letter:
        print('correct')
    else:
        print('wrong answer')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thank you for your answer.

The problem with your code is: it re-starts the game.
What I need is: do not restart the game. Take the 2nd input and consider as 1st and keep asking for 2nd input.

For "# continue with whatever" in below, is there a way to say Python to "go to a specific line" in case "if" condition is met ?

Thanks

Quote:
if second_data == first_data:
    # continue with whatever
else:
    print('you lost')

(Jul-14-2022, 07:28 PM)menator01 Wrote: There is no condition for the you lost line. It will always print.
you will need something like
if second_data == first_data:
    # continue with whatever
else:
    print('you lost')
On a side note that should probably be in the while loop


Here is an example for you to play around with

while True:
    print('ctrl c to quit')
    first = input('country name: ')
    second = input('another name: ')

    last_letter = first[-1]
    first_letter = second[:1]

    if last_letter == first_letter:
        print('correct')
    else:
        print('wrong answer')
Reply
#4
(Jul-15-2022, 08:50 AM)tylerdurdane Wrote: What I need is: do not restart the game. Take the 2nd input and consider as 1st and keep asking for 2nd input.
So, to clarify... Is something like this what your looking for?
first_data=input('enter a country name: ')
print(first_data)
last_letter=str(first_data[-1])
print(last_letter)
second_data = 'Wrong answer!'
while second_data [-1] != last_letter :
	second_data=input('enter a country name starts with the last letter of previous country name: ')
Reply
#5
Quote:So, to clarify... Is something like this what your looking for?
Hi,
Not excatly. I tried your code. Doesnt matter if I enter correct or wrong answer, it keeps asking for the 2nd input.
In your code I couldnt understand below part. Not sure what second_data should be but definitly not like below
Quote:second_data = 'Wrong answer!'
I tried to modify your code however not successful

The game should be like this example: Nigeria --> Azerbaijan --> Nepal --> Laos --> Slovenia --> Argentina --> etc.
Reply
#6
This is the output I get when the answer is correct.
Output:
enter a country name: africa africa a enter a country name starts with the last letter of previous country name: america
This is the answer I get when the answer is wrong.
Output:
enter a country name: africa africa a enter a country name starts with the last letter of previous country name: france enter a country name starts with the last letter of previous country name: germiny enter a country name starts with the last letter of previous country name:
If you are capitalizing the names, you will have to compensate for that.
The codesecond_data = 'Wrong answer!'is just initializing the variable. It could just as well mesecond_data = "z".
Reply
#7
Ok can you modify the code to exit if you reply wrong ?
Thank you

Quote:This is the answer I get when the answer is wrong.
Output:
enter a country name: africa africa a enter a country name starts with the last letter of previous country name: france enter a country name starts with the last letter of previous country name: germiny enter a country name starts with the last letter of previous country name:
Reply
#8
(Jul-15-2022, 04:20 PM)tylerdurdane Wrote: Ok can you modify the code to exit if you reply wrong ?
and keep going if you reply correctly or quit either way?
Reply
#9
Okay, sorry. I think I understand now. Try this:
first_data=input('enter a country name: ')
while True :
	print(first_data)
	last_letter=str(first_data[-1]).lower ()
	print(last_letter)
	second_data=input('enter a country name starts with the last letter of previous country name: ')
	if second_data [0].lower () == last_letter :
		first_data = second_data
	else : break
print ('You lose!')
menator01 likes this post
Reply
#10
Thumbs Up 
I tried the code, seems like this is what I was looking for.
That's great!
Thanks
Cheers
I asked the same question in stackoverflow, so I will share this link there

(Jul-15-2022, 06:43 PM)BashBedlam Wrote: Okay, sorry. I think I understand now. Try this:
first_data=input('enter a country name: ')
while True :
	print(first_data)
	last_letter=str(first_data[-1]).lower ()
	print(last_letter)
	second_data=input('enter a country name starts with the last letter of previous country name: ')
	if second_data [0].lower () == last_letter :
		first_data = second_data
	else : break
print ('You lose!')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Waiting screen until user input - PiBooth world90 2 893 Apr-16-2024, 03:53 PM
Last Post: attractalderman
  [PyGame] adding mouse control to game flash77 2 198 Apr-08-2024, 06:52 PM
Last Post: flash77
  Isometric game pygame Tiled howardberger 1 582 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  I want to add a bullet function to my game. how would i go about it? Iyjja 5 894 Jan-09-2024, 05:42 PM
Last Post: Iyjja
  GPS Points to map for game/simulator AvgPlum 2 1,942 Jun-22-2023, 06:38 PM
Last Post: marynetherton
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 984 Apr-30-2023, 10:11 PM
Last Post: deanhystad
  Laggy Game Movement game_slayer_99 12 4,322 Oct-05-2022, 11:34 AM
Last Post: metulburr
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,772 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] AI Techniques applied to a Game insignia47 1 1,485 Jun-17-2022, 05:25 PM
Last Post: deanhystad
  A 3D game engine markcopperman78 5 5,953 Jun-08-2022, 10:43 PM
Last Post: netpipe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020