Python Forum
If 2nd input in the game is correct, replace with 1st input and loop the game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: If 2nd input in the game is correct, replace with 1st input and loop the game (/thread-37730.html)

Pages: 1 2


If 2nd input in the game is correct, replace with 1st input and loop the game - tylerdurdane - Jul-14-2022

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



RE: If 2nd input in the game is correct, replace with 1st input and loop the game - menator01 - Jul-14-2022

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')



RE: If 2nd input in the game is correct, replace with 1st input and loop the game - tylerdurdane - Jul-15-2022

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')



RE: If 2nd input in the game is correct, replace with 1st input and loop the game - BashBedlam - Jul-15-2022

(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: ')



RE: If 2nd input in the game is correct, replace with 1st input and loop the game - tylerdurdane - Jul-15-2022

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.


RE: If 2nd input in the game is correct, replace with 1st input and loop the game - BashBedlam - Jul-15-2022

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".


RE: If 2nd input in the game is correct, replace with 1st input and loop the game - tylerdurdane - Jul-15-2022

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:



RE: If 2nd input in the game is correct, replace with 1st input and loop the game - BashBedlam - Jul-15-2022

(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?


RE: If 2nd input in the game is correct, replace with 1st input and loop the game - BashBedlam - Jul-15-2022

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!')



RE: If 2nd input in the game is correct, replace with 1st input and loop the game - tylerdurdane - Jul-16-2022

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!')